1*0Sstevel@tonic-gate /* ssl/s2_lib.c */
2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * This package is an SSL implementation written
6*0Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
7*0Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
10*0Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
11*0Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
12*0Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*0Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
14*0Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
17*0Sstevel@tonic-gate  * the code are not to be removed.
18*0Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
19*0Sstevel@tonic-gate  * as the author of the parts of the library used.
20*0Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
21*0Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
24*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
25*0Sstevel@tonic-gate  * are met:
26*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
27*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
28*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
29*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
30*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
31*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
32*0Sstevel@tonic-gate  *    must display the following acknowledgement:
33*0Sstevel@tonic-gate  *    "This product includes cryptographic software written by
34*0Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
35*0Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
36*0Sstevel@tonic-gate  *    being used are not cryptographic related :-).
37*0Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
38*0Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
39*0Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*0Sstevel@tonic-gate  * SUCH DAMAGE.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
54*0Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*0Sstevel@tonic-gate  * copied and put under another distribution licence
56*0Sstevel@tonic-gate  * [including the GNU Public Licence.]
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include "ssl_locl.h"
60*0Sstevel@tonic-gate #ifndef OPENSSL_NO_SSL2
61*0Sstevel@tonic-gate #include <stdio.h>
62*0Sstevel@tonic-gate #include <openssl/rsa.h>
63*0Sstevel@tonic-gate #include <openssl/objects.h>
64*0Sstevel@tonic-gate #include <openssl/evp.h>
65*0Sstevel@tonic-gate #include <openssl/md5.h>
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate static long ssl2_default_timeout(void );
68*0Sstevel@tonic-gate const char *ssl2_version_str="SSLv2" OPENSSL_VERSION_PTEXT;
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate #define SSL2_NUM_CIPHERS (sizeof(ssl2_ciphers)/sizeof(SSL_CIPHER))
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate OPENSSL_GLOBAL SSL_CIPHER ssl2_ciphers[]={
73*0Sstevel@tonic-gate /* NULL_WITH_MD5 v3 */
74*0Sstevel@tonic-gate #if 0
75*0Sstevel@tonic-gate 	{
76*0Sstevel@tonic-gate 	1,
77*0Sstevel@tonic-gate 	SSL2_TXT_NULL_WITH_MD5,
78*0Sstevel@tonic-gate 	SSL2_CK_NULL_WITH_MD5,
79*0Sstevel@tonic-gate 	SSL_kRSA|SSL_aRSA|SSL_eNULL|SSL_MD5|SSL_SSLV2,
80*0Sstevel@tonic-gate 	SSL_EXPORT|SSL_EXP40|SSL_STRONG_NONE,
81*0Sstevel@tonic-gate 	0,
82*0Sstevel@tonic-gate 	0,
83*0Sstevel@tonic-gate 	0,
84*0Sstevel@tonic-gate 	SSL_ALL_CIPHERS,
85*0Sstevel@tonic-gate 	SSL_ALL_STRENGTHS,
86*0Sstevel@tonic-gate 	},
87*0Sstevel@tonic-gate #endif
88*0Sstevel@tonic-gate /* RC4_128_EXPORT40_WITH_MD5 */
89*0Sstevel@tonic-gate 	{
90*0Sstevel@tonic-gate 	1,
91*0Sstevel@tonic-gate 	SSL2_TXT_RC4_128_EXPORT40_WITH_MD5,
92*0Sstevel@tonic-gate 	SSL2_CK_RC4_128_EXPORT40_WITH_MD5,
93*0Sstevel@tonic-gate 	SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_SSLV2,
94*0Sstevel@tonic-gate 	SSL_EXPORT|SSL_EXP40,
95*0Sstevel@tonic-gate 	SSL2_CF_5_BYTE_ENC,
96*0Sstevel@tonic-gate 	40,
97*0Sstevel@tonic-gate 	128,
98*0Sstevel@tonic-gate 	SSL_ALL_CIPHERS,
99*0Sstevel@tonic-gate 	SSL_ALL_STRENGTHS,
100*0Sstevel@tonic-gate 	},
101*0Sstevel@tonic-gate /* RC4_128_WITH_MD5 */
102*0Sstevel@tonic-gate 	{
103*0Sstevel@tonic-gate 	1,
104*0Sstevel@tonic-gate 	SSL2_TXT_RC4_128_WITH_MD5,
105*0Sstevel@tonic-gate 	SSL2_CK_RC4_128_WITH_MD5,
106*0Sstevel@tonic-gate 	SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_SSLV2,
107*0Sstevel@tonic-gate 	SSL_NOT_EXP|SSL_MEDIUM,
108*0Sstevel@tonic-gate 	0,
109*0Sstevel@tonic-gate 	128,
110*0Sstevel@tonic-gate 	128,
111*0Sstevel@tonic-gate 	SSL_ALL_CIPHERS,
112*0Sstevel@tonic-gate 	SSL_ALL_STRENGTHS,
113*0Sstevel@tonic-gate 	},
114*0Sstevel@tonic-gate /* RC2_128_CBC_EXPORT40_WITH_MD5 */
115*0Sstevel@tonic-gate 	{
116*0Sstevel@tonic-gate 	1,
117*0Sstevel@tonic-gate 	SSL2_TXT_RC2_128_CBC_EXPORT40_WITH_MD5,
118*0Sstevel@tonic-gate 	SSL2_CK_RC2_128_CBC_EXPORT40_WITH_MD5,
119*0Sstevel@tonic-gate 	SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5|SSL_SSLV2,
120*0Sstevel@tonic-gate 	SSL_EXPORT|SSL_EXP40,
121*0Sstevel@tonic-gate 	SSL2_CF_5_BYTE_ENC,
122*0Sstevel@tonic-gate 	40,
123*0Sstevel@tonic-gate 	128,
124*0Sstevel@tonic-gate 	SSL_ALL_CIPHERS,
125*0Sstevel@tonic-gate 	SSL_ALL_STRENGTHS,
126*0Sstevel@tonic-gate 	},
127*0Sstevel@tonic-gate /* RC2_128_CBC_WITH_MD5 */
128*0Sstevel@tonic-gate 	{
129*0Sstevel@tonic-gate 	1,
130*0Sstevel@tonic-gate 	SSL2_TXT_RC2_128_CBC_WITH_MD5,
131*0Sstevel@tonic-gate 	SSL2_CK_RC2_128_CBC_WITH_MD5,
132*0Sstevel@tonic-gate 	SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5|SSL_SSLV2,
133*0Sstevel@tonic-gate 	SSL_NOT_EXP|SSL_MEDIUM,
134*0Sstevel@tonic-gate 	0,
135*0Sstevel@tonic-gate 	128,
136*0Sstevel@tonic-gate 	128,
137*0Sstevel@tonic-gate 	SSL_ALL_CIPHERS,
138*0Sstevel@tonic-gate 	SSL_ALL_STRENGTHS,
139*0Sstevel@tonic-gate 	},
140*0Sstevel@tonic-gate /* IDEA_128_CBC_WITH_MD5 */
141*0Sstevel@tonic-gate #ifndef OPENSSL_NO_IDEA
142*0Sstevel@tonic-gate 	{
143*0Sstevel@tonic-gate 	1,
144*0Sstevel@tonic-gate 	SSL2_TXT_IDEA_128_CBC_WITH_MD5,
145*0Sstevel@tonic-gate 	SSL2_CK_IDEA_128_CBC_WITH_MD5,
146*0Sstevel@tonic-gate 	SSL_kRSA|SSL_aRSA|SSL_IDEA|SSL_MD5|SSL_SSLV2,
147*0Sstevel@tonic-gate 	SSL_NOT_EXP|SSL_MEDIUM,
148*0Sstevel@tonic-gate 	0,
149*0Sstevel@tonic-gate 	128,
150*0Sstevel@tonic-gate 	128,
151*0Sstevel@tonic-gate 	SSL_ALL_CIPHERS,
152*0Sstevel@tonic-gate 	SSL_ALL_STRENGTHS,
153*0Sstevel@tonic-gate 	},
154*0Sstevel@tonic-gate #endif
155*0Sstevel@tonic-gate /* DES_64_CBC_WITH_MD5 */
156*0Sstevel@tonic-gate 	{
157*0Sstevel@tonic-gate 	1,
158*0Sstevel@tonic-gate 	SSL2_TXT_DES_64_CBC_WITH_MD5,
159*0Sstevel@tonic-gate 	SSL2_CK_DES_64_CBC_WITH_MD5,
160*0Sstevel@tonic-gate 	SSL_kRSA|SSL_aRSA|SSL_DES|SSL_MD5|SSL_SSLV2,
161*0Sstevel@tonic-gate 	SSL_NOT_EXP|SSL_LOW,
162*0Sstevel@tonic-gate 	0,
163*0Sstevel@tonic-gate 	56,
164*0Sstevel@tonic-gate 	56,
165*0Sstevel@tonic-gate 	SSL_ALL_CIPHERS,
166*0Sstevel@tonic-gate 	SSL_ALL_STRENGTHS,
167*0Sstevel@tonic-gate 	},
168*0Sstevel@tonic-gate /* DES_192_EDE3_CBC_WITH_MD5 */
169*0Sstevel@tonic-gate 	{
170*0Sstevel@tonic-gate 	1,
171*0Sstevel@tonic-gate 	SSL2_TXT_DES_192_EDE3_CBC_WITH_MD5,
172*0Sstevel@tonic-gate 	SSL2_CK_DES_192_EDE3_CBC_WITH_MD5,
173*0Sstevel@tonic-gate 	SSL_kRSA|SSL_aRSA|SSL_3DES|SSL_MD5|SSL_SSLV2,
174*0Sstevel@tonic-gate 	SSL_NOT_EXP|SSL_HIGH,
175*0Sstevel@tonic-gate 	0,
176*0Sstevel@tonic-gate 	168,
177*0Sstevel@tonic-gate 	168,
178*0Sstevel@tonic-gate 	SSL_ALL_CIPHERS,
179*0Sstevel@tonic-gate 	SSL_ALL_STRENGTHS,
180*0Sstevel@tonic-gate 	},
181*0Sstevel@tonic-gate /* RC4_64_WITH_MD5 */
182*0Sstevel@tonic-gate #if 1
183*0Sstevel@tonic-gate 	{
184*0Sstevel@tonic-gate 	1,
185*0Sstevel@tonic-gate 	SSL2_TXT_RC4_64_WITH_MD5,
186*0Sstevel@tonic-gate 	SSL2_CK_RC4_64_WITH_MD5,
187*0Sstevel@tonic-gate 	SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_SSLV2,
188*0Sstevel@tonic-gate 	SSL_NOT_EXP|SSL_LOW,
189*0Sstevel@tonic-gate 	SSL2_CF_8_BYTE_ENC,
190*0Sstevel@tonic-gate 	64,
191*0Sstevel@tonic-gate 	64,
192*0Sstevel@tonic-gate 	SSL_ALL_CIPHERS,
193*0Sstevel@tonic-gate 	SSL_ALL_STRENGTHS,
194*0Sstevel@tonic-gate 	},
195*0Sstevel@tonic-gate #endif
196*0Sstevel@tonic-gate /* NULL SSLeay (testing) */
197*0Sstevel@tonic-gate #if 0
198*0Sstevel@tonic-gate 	{
199*0Sstevel@tonic-gate 	0,
200*0Sstevel@tonic-gate 	SSL2_TXT_NULL,
201*0Sstevel@tonic-gate 	SSL2_CK_NULL,
202*0Sstevel@tonic-gate 	0,
203*0Sstevel@tonic-gate 	SSL_STRONG_NONE,
204*0Sstevel@tonic-gate 	0,
205*0Sstevel@tonic-gate 	0,
206*0Sstevel@tonic-gate 	0,
207*0Sstevel@tonic-gate 	SSL_ALL_CIPHERS,
208*0Sstevel@tonic-gate 	SSL_ALL_STRENGTHS,
209*0Sstevel@tonic-gate 	},
210*0Sstevel@tonic-gate #endif
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate /* end of list :-) */
213*0Sstevel@tonic-gate 	};
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate static SSL_METHOD SSLv2_data= {
216*0Sstevel@tonic-gate 	SSL2_VERSION,
217*0Sstevel@tonic-gate 	ssl2_new,	/* local */
218*0Sstevel@tonic-gate 	ssl2_clear,	/* local */
219*0Sstevel@tonic-gate 	ssl2_free,	/* local */
220*0Sstevel@tonic-gate 	ssl_undefined_function,
221*0Sstevel@tonic-gate 	ssl_undefined_function,
222*0Sstevel@tonic-gate 	ssl2_read,
223*0Sstevel@tonic-gate 	ssl2_peek,
224*0Sstevel@tonic-gate 	ssl2_write,
225*0Sstevel@tonic-gate 	ssl2_shutdown,
226*0Sstevel@tonic-gate 	ssl_ok,	/* NULL - renegotiate */
227*0Sstevel@tonic-gate 	ssl_ok,	/* NULL - check renegotiate */
228*0Sstevel@tonic-gate 	ssl2_ctrl,	/* local */
229*0Sstevel@tonic-gate 	ssl2_ctx_ctrl,	/* local */
230*0Sstevel@tonic-gate 	ssl2_get_cipher_by_char,
231*0Sstevel@tonic-gate 	ssl2_put_cipher_by_char,
232*0Sstevel@tonic-gate 	ssl2_pending,
233*0Sstevel@tonic-gate 	ssl2_num_ciphers,
234*0Sstevel@tonic-gate 	ssl2_get_cipher,
235*0Sstevel@tonic-gate 	ssl_bad_method,
236*0Sstevel@tonic-gate 	ssl2_default_timeout,
237*0Sstevel@tonic-gate 	&ssl3_undef_enc_method,
238*0Sstevel@tonic-gate 	ssl_undefined_function,
239*0Sstevel@tonic-gate 	ssl2_callback_ctrl,	/* local */
240*0Sstevel@tonic-gate 	ssl2_ctx_callback_ctrl,	/* local */
241*0Sstevel@tonic-gate 	};
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate static long ssl2_default_timeout(void)
244*0Sstevel@tonic-gate 	{
245*0Sstevel@tonic-gate 	return(300);
246*0Sstevel@tonic-gate 	}
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate SSL_METHOD *sslv2_base_method(void)
249*0Sstevel@tonic-gate 	{
250*0Sstevel@tonic-gate 	return(&SSLv2_data);
251*0Sstevel@tonic-gate 	}
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate int ssl2_num_ciphers(void)
254*0Sstevel@tonic-gate 	{
255*0Sstevel@tonic-gate 	return(SSL2_NUM_CIPHERS);
256*0Sstevel@tonic-gate 	}
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate SSL_CIPHER *ssl2_get_cipher(unsigned int u)
259*0Sstevel@tonic-gate 	{
260*0Sstevel@tonic-gate 	if (u < SSL2_NUM_CIPHERS)
261*0Sstevel@tonic-gate 		return(&(ssl2_ciphers[SSL2_NUM_CIPHERS-1-u]));
262*0Sstevel@tonic-gate 	else
263*0Sstevel@tonic-gate 		return(NULL);
264*0Sstevel@tonic-gate 	}
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate int ssl2_pending(SSL *s)
267*0Sstevel@tonic-gate 	{
268*0Sstevel@tonic-gate 	return SSL_in_init(s) ? 0 : s->s2->ract_data_length;
269*0Sstevel@tonic-gate 	}
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate int ssl2_new(SSL *s)
272*0Sstevel@tonic-gate 	{
273*0Sstevel@tonic-gate 	SSL2_STATE *s2;
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	if ((s2=OPENSSL_malloc(sizeof *s2)) == NULL) goto err;
276*0Sstevel@tonic-gate 	memset(s2,0,sizeof *s2);
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate #if SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER + 3 > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER + 2
279*0Sstevel@tonic-gate #  error "assertion failed"
280*0Sstevel@tonic-gate #endif
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	if ((s2->rbuf=OPENSSL_malloc(
283*0Sstevel@tonic-gate 		SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2)) == NULL) goto err;
284*0Sstevel@tonic-gate 	/* wbuf needs one byte more because when using two-byte headers,
285*0Sstevel@tonic-gate 	 * we leave the first byte unused in do_ssl_write (s2_pkt.c) */
286*0Sstevel@tonic-gate 	if ((s2->wbuf=OPENSSL_malloc(
287*0Sstevel@tonic-gate 		SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+3)) == NULL) goto err;
288*0Sstevel@tonic-gate 	s->s2=s2;
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	ssl2_clear(s);
291*0Sstevel@tonic-gate 	return(1);
292*0Sstevel@tonic-gate err:
293*0Sstevel@tonic-gate 	if (s2 != NULL)
294*0Sstevel@tonic-gate 		{
295*0Sstevel@tonic-gate 		if (s2->wbuf != NULL) OPENSSL_free(s2->wbuf);
296*0Sstevel@tonic-gate 		if (s2->rbuf != NULL) OPENSSL_free(s2->rbuf);
297*0Sstevel@tonic-gate 		OPENSSL_free(s2);
298*0Sstevel@tonic-gate 		}
299*0Sstevel@tonic-gate 	return(0);
300*0Sstevel@tonic-gate 	}
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate void ssl2_free(SSL *s)
303*0Sstevel@tonic-gate 	{
304*0Sstevel@tonic-gate 	SSL2_STATE *s2;
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 	if(s == NULL)
307*0Sstevel@tonic-gate 	    return;
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 	s2=s->s2;
310*0Sstevel@tonic-gate 	if (s2->rbuf != NULL) OPENSSL_free(s2->rbuf);
311*0Sstevel@tonic-gate 	if (s2->wbuf != NULL) OPENSSL_free(s2->wbuf);
312*0Sstevel@tonic-gate 	OPENSSL_cleanse(s2,sizeof *s2);
313*0Sstevel@tonic-gate 	OPENSSL_free(s2);
314*0Sstevel@tonic-gate 	s->s2=NULL;
315*0Sstevel@tonic-gate 	}
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate void ssl2_clear(SSL *s)
318*0Sstevel@tonic-gate 	{
319*0Sstevel@tonic-gate 	SSL2_STATE *s2;
320*0Sstevel@tonic-gate 	unsigned char *rbuf,*wbuf;
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 	s2=s->s2;
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate 	rbuf=s2->rbuf;
325*0Sstevel@tonic-gate 	wbuf=s2->wbuf;
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 	memset(s2,0,sizeof *s2);
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 	s2->rbuf=rbuf;
330*0Sstevel@tonic-gate 	s2->wbuf=wbuf;
331*0Sstevel@tonic-gate 	s2->clear_text=1;
332*0Sstevel@tonic-gate 	s->packet=s2->rbuf;
333*0Sstevel@tonic-gate 	s->version=SSL2_VERSION;
334*0Sstevel@tonic-gate 	s->packet_length=0;
335*0Sstevel@tonic-gate 	}
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate long ssl2_ctrl(SSL *s, int cmd, long larg, void *parg)
338*0Sstevel@tonic-gate 	{
339*0Sstevel@tonic-gate 	int ret=0;
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 	switch(cmd)
342*0Sstevel@tonic-gate 		{
343*0Sstevel@tonic-gate 	case SSL_CTRL_GET_SESSION_REUSED:
344*0Sstevel@tonic-gate 		ret=s->hit;
345*0Sstevel@tonic-gate 		break;
346*0Sstevel@tonic-gate 	default:
347*0Sstevel@tonic-gate 		break;
348*0Sstevel@tonic-gate 		}
349*0Sstevel@tonic-gate 	return(ret);
350*0Sstevel@tonic-gate 	}
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate long ssl2_callback_ctrl(SSL *s, int cmd, void (*fp)())
353*0Sstevel@tonic-gate 	{
354*0Sstevel@tonic-gate 	return(0);
355*0Sstevel@tonic-gate 	}
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate long ssl2_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
358*0Sstevel@tonic-gate 	{
359*0Sstevel@tonic-gate 	return(0);
360*0Sstevel@tonic-gate 	}
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate long ssl2_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)())
363*0Sstevel@tonic-gate 	{
364*0Sstevel@tonic-gate 	return(0);
365*0Sstevel@tonic-gate 	}
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate /* This function needs to check if the ciphers required are actually
368*0Sstevel@tonic-gate  * available */
369*0Sstevel@tonic-gate SSL_CIPHER *ssl2_get_cipher_by_char(const unsigned char *p)
370*0Sstevel@tonic-gate 	{
371*0Sstevel@tonic-gate 	static int init=1;
372*0Sstevel@tonic-gate 	static SSL_CIPHER *sorted[SSL2_NUM_CIPHERS];
373*0Sstevel@tonic-gate 	SSL_CIPHER c,*cp= &c,**cpp;
374*0Sstevel@tonic-gate 	unsigned long id;
375*0Sstevel@tonic-gate 	int i;
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	if (init)
378*0Sstevel@tonic-gate 		{
379*0Sstevel@tonic-gate 		CRYPTO_w_lock(CRYPTO_LOCK_SSL);
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 		if (init)
382*0Sstevel@tonic-gate 			{
383*0Sstevel@tonic-gate 			for (i=0; i<SSL2_NUM_CIPHERS; i++)
384*0Sstevel@tonic-gate 				sorted[i]= &(ssl2_ciphers[i]);
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 			qsort((char *)sorted,
387*0Sstevel@tonic-gate 				SSL2_NUM_CIPHERS,sizeof(SSL_CIPHER *),
388*0Sstevel@tonic-gate 				FP_ICC ssl_cipher_ptr_id_cmp);
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 			init=0;
391*0Sstevel@tonic-gate 			}
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate 		CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
394*0Sstevel@tonic-gate 		}
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	id=0x02000000L|((unsigned long)p[0]<<16L)|
397*0Sstevel@tonic-gate 		((unsigned long)p[1]<<8L)|(unsigned long)p[2];
398*0Sstevel@tonic-gate 	c.id=id;
399*0Sstevel@tonic-gate 	cpp=(SSL_CIPHER **)OBJ_bsearch((char *)&cp,
400*0Sstevel@tonic-gate 		(char *)sorted,
401*0Sstevel@tonic-gate 		SSL2_NUM_CIPHERS,sizeof(SSL_CIPHER *),
402*0Sstevel@tonic-gate 		FP_ICC ssl_cipher_ptr_id_cmp);
403*0Sstevel@tonic-gate 	if ((cpp == NULL) || !(*cpp)->valid)
404*0Sstevel@tonic-gate 		return(NULL);
405*0Sstevel@tonic-gate 	else
406*0Sstevel@tonic-gate 		return(*cpp);
407*0Sstevel@tonic-gate 	}
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate int ssl2_put_cipher_by_char(const SSL_CIPHER *c, unsigned char *p)
410*0Sstevel@tonic-gate 	{
411*0Sstevel@tonic-gate 	long l;
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 	if (p != NULL)
414*0Sstevel@tonic-gate 		{
415*0Sstevel@tonic-gate 		l=c->id;
416*0Sstevel@tonic-gate 		if ((l & 0xff000000) != 0x02000000) return(0);
417*0Sstevel@tonic-gate 		p[0]=((unsigned char)(l>>16L))&0xFF;
418*0Sstevel@tonic-gate 		p[1]=((unsigned char)(l>> 8L))&0xFF;
419*0Sstevel@tonic-gate 		p[2]=((unsigned char)(l     ))&0xFF;
420*0Sstevel@tonic-gate 		}
421*0Sstevel@tonic-gate 	return(3);
422*0Sstevel@tonic-gate 	}
423*0Sstevel@tonic-gate 
424*0Sstevel@tonic-gate int ssl2_generate_key_material(SSL *s)
425*0Sstevel@tonic-gate 	{
426*0Sstevel@tonic-gate 	unsigned int i;
427*0Sstevel@tonic-gate 	EVP_MD_CTX ctx;
428*0Sstevel@tonic-gate 	unsigned char *km;
429*0Sstevel@tonic-gate 	unsigned char c='0';
430*0Sstevel@tonic-gate 	const EVP_MD *md5;
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate 	md5 = EVP_md5();
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate #ifdef CHARSET_EBCDIC
435*0Sstevel@tonic-gate 	c = os_toascii['0']; /* Must be an ASCII '0', not EBCDIC '0',
436*0Sstevel@tonic-gate 				see SSLv2 docu */
437*0Sstevel@tonic-gate #endif
438*0Sstevel@tonic-gate 	EVP_MD_CTX_init(&ctx);
439*0Sstevel@tonic-gate 	km=s->s2->key_material;
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate  	if (s->session->master_key_length < 0 || s->session->master_key_length > sizeof s->session->master_key)
442*0Sstevel@tonic-gate  		{
443*0Sstevel@tonic-gate  		SSLerr(SSL_F_SSL2_GENERATE_KEY_MATERIAL, ERR_R_INTERNAL_ERROR);
444*0Sstevel@tonic-gate  		return 0;
445*0Sstevel@tonic-gate  		}
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 	for (i=0; i<s->s2->key_material_length; i += EVP_MD_size(md5))
448*0Sstevel@tonic-gate 		{
449*0Sstevel@tonic-gate 		if (((km - s->s2->key_material) + EVP_MD_size(md5)) > sizeof s->s2->key_material)
450*0Sstevel@tonic-gate 			{
451*0Sstevel@tonic-gate 			/* EVP_DigestFinal_ex() below would write beyond buffer */
452*0Sstevel@tonic-gate 			SSLerr(SSL_F_SSL2_GENERATE_KEY_MATERIAL, ERR_R_INTERNAL_ERROR);
453*0Sstevel@tonic-gate 			return 0;
454*0Sstevel@tonic-gate 			}
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 		EVP_DigestInit_ex(&ctx, md5, NULL);
457*0Sstevel@tonic-gate 
458*0Sstevel@tonic-gate 		OPENSSL_assert(s->session->master_key_length >= 0
459*0Sstevel@tonic-gate 		    && s->session->master_key_length
460*0Sstevel@tonic-gate 		    < sizeof s->session->master_key);
461*0Sstevel@tonic-gate 		EVP_DigestUpdate(&ctx,s->session->master_key,s->session->master_key_length);
462*0Sstevel@tonic-gate 		EVP_DigestUpdate(&ctx,&c,1);
463*0Sstevel@tonic-gate 		c++;
464*0Sstevel@tonic-gate 		EVP_DigestUpdate(&ctx,s->s2->challenge,s->s2->challenge_length);
465*0Sstevel@tonic-gate 		EVP_DigestUpdate(&ctx,s->s2->conn_id,s->s2->conn_id_length);
466*0Sstevel@tonic-gate 		EVP_DigestFinal_ex(&ctx,km,NULL);
467*0Sstevel@tonic-gate 		km += EVP_MD_size(md5);
468*0Sstevel@tonic-gate 		}
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 	EVP_MD_CTX_cleanup(&ctx);
471*0Sstevel@tonic-gate 	return 1;
472*0Sstevel@tonic-gate 	}
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate void ssl2_return_error(SSL *s, int err)
475*0Sstevel@tonic-gate 	{
476*0Sstevel@tonic-gate 	if (!s->error)
477*0Sstevel@tonic-gate 		{
478*0Sstevel@tonic-gate 		s->error=3;
479*0Sstevel@tonic-gate 		s->error_code=err;
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate 		ssl2_write_error(s);
482*0Sstevel@tonic-gate 		}
483*0Sstevel@tonic-gate 	}
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate void ssl2_write_error(SSL *s)
487*0Sstevel@tonic-gate 	{
488*0Sstevel@tonic-gate 	unsigned char buf[3];
489*0Sstevel@tonic-gate 	int i,error;
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate 	buf[0]=SSL2_MT_ERROR;
492*0Sstevel@tonic-gate 	buf[1]=(s->error_code>>8)&0xff;
493*0Sstevel@tonic-gate 	buf[2]=(s->error_code)&0xff;
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate /*	state=s->rwstate;*/
496*0Sstevel@tonic-gate 
497*0Sstevel@tonic-gate 	error=s->error; /* number of bytes left to write */
498*0Sstevel@tonic-gate 	s->error=0;
499*0Sstevel@tonic-gate 	OPENSSL_assert(error >= 0 && error <= sizeof buf);
500*0Sstevel@tonic-gate 	i=ssl2_write(s,&(buf[3-error]),error);
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate /*	if (i == error) s->rwstate=state; */
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 	if (i < 0)
505*0Sstevel@tonic-gate 		s->error=error;
506*0Sstevel@tonic-gate 	else
507*0Sstevel@tonic-gate 		{
508*0Sstevel@tonic-gate 		s->error=error-i;
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 		if (s->error == 0)
511*0Sstevel@tonic-gate 			if (s->msg_callback)
512*0Sstevel@tonic-gate 				s->msg_callback(1, s->version, 0, buf, 3, s, s->msg_callback_arg); /* ERROR */
513*0Sstevel@tonic-gate 		}
514*0Sstevel@tonic-gate 	}
515*0Sstevel@tonic-gate 
516*0Sstevel@tonic-gate int ssl2_shutdown(SSL *s)
517*0Sstevel@tonic-gate 	{
518*0Sstevel@tonic-gate 	s->shutdown=(SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
519*0Sstevel@tonic-gate 	return(1);
520*0Sstevel@tonic-gate 	}
521*0Sstevel@tonic-gate #else /* !OPENSSL_NO_SSL2 */
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate # if PEDANTIC
524*0Sstevel@tonic-gate static void *dummy=&dummy;
525*0Sstevel@tonic-gate # endif
526*0Sstevel@tonic-gate 
527*0Sstevel@tonic-gate #endif
528