Broken HPUX cc.
[openssl.git] / ssl / s2_lib.c
1 /* ssl/s2_lib.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 #ifndef NO_RSA
60 #include <stdio.h>
61 #include <openssl/rsa.h>
62 #include <openssl/objects.h>
63 #include <openssl/md5.h>
64 #include "ssl_locl.h"
65
66 static long ssl2_default_timeout(void );
67 const char *ssl2_version_str="SSLv2" OPENSSL_VERSION_PTEXT;
68
69 #define SSL2_NUM_CIPHERS (sizeof(ssl2_ciphers)/sizeof(SSL_CIPHER))
70
71 OPENSSL_GLOBAL SSL_CIPHER ssl2_ciphers[]={
72 /* NULL_WITH_MD5 v3 */
73 #if 0
74         {
75         1,
76         SSL2_TXT_NULL_WITH_MD5,
77         SSL2_CK_NULL_WITH_MD5,
78         SSL_kRSA|SSL_aRSA|SSL_eNULL|SSL_MD5|SSL_EXP40|SSL_SSLV2,
79         0,
80         SSL_ALL_CIPHERS,
81         },
82 #endif
83 /* RC4_128_EXPORT40_WITH_MD5 */
84         {
85         1,
86         SSL2_TXT_RC4_128_EXPORT40_WITH_MD5,
87         SSL2_CK_RC4_128_EXPORT40_WITH_MD5,
88         SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_EXP40|SSL_SSLV2,
89         SSL2_CF_5_BYTE_ENC,
90         SSL_ALL_CIPHERS,
91         },
92 /* RC4_128_WITH_MD5 */
93         {
94         1,
95         SSL2_TXT_RC4_128_WITH_MD5,
96         SSL2_CK_RC4_128_WITH_MD5,
97         SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_NOT_EXP|SSL_SSLV2|SSL_MEDIUM,
98         0,
99         SSL_ALL_CIPHERS,
100         },
101 /* RC2_128_CBC_EXPORT40_WITH_MD5 */
102         {
103         1,
104         SSL2_TXT_RC2_128_CBC_EXPORT40_WITH_MD5,
105         SSL2_CK_RC2_128_CBC_EXPORT40_WITH_MD5,
106         SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5|SSL_EXP40|SSL_SSLV2,
107         SSL2_CF_5_BYTE_ENC,
108         SSL_ALL_CIPHERS,
109         },
110 /* RC2_128_CBC_WITH_MD5 */
111         {
112         1,
113         SSL2_TXT_RC2_128_CBC_WITH_MD5,
114         SSL2_CK_RC2_128_CBC_WITH_MD5,
115         SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5|SSL_NOT_EXP|SSL_SSLV2|SSL_MEDIUM,
116         0,
117         SSL_ALL_CIPHERS,
118         },
119 /* IDEA_128_CBC_WITH_MD5 */
120         {
121         1,
122         SSL2_TXT_IDEA_128_CBC_WITH_MD5,
123         SSL2_CK_IDEA_128_CBC_WITH_MD5,
124         SSL_kRSA|SSL_aRSA|SSL_IDEA|SSL_MD5|SSL_NOT_EXP|SSL_SSLV2|SSL_MEDIUM,
125         0,
126         SSL_ALL_CIPHERS,
127         },
128 /* DES_64_CBC_WITH_MD5 */
129         {
130         1,
131         SSL2_TXT_DES_64_CBC_WITH_MD5,
132         SSL2_CK_DES_64_CBC_WITH_MD5,
133         SSL_kRSA|SSL_aRSA|SSL_DES|SSL_MD5|SSL_NOT_EXP|SSL_SSLV2|SSL_LOW,
134         0,
135         SSL_ALL_CIPHERS,
136         },
137 /* DES_192_EDE3_CBC_WITH_MD5 */
138         {
139         1,
140         SSL2_TXT_DES_192_EDE3_CBC_WITH_MD5,
141         SSL2_CK_DES_192_EDE3_CBC_WITH_MD5,
142         SSL_kRSA|SSL_aRSA|SSL_3DES|SSL_MD5|SSL_NOT_EXP|SSL_SSLV2|SSL_HIGH,
143         0,
144         SSL_ALL_CIPHERS,
145         },
146 /* RC4_64_WITH_MD5 */
147 #if 1
148         {
149         1,
150         SSL2_TXT_RC4_64_WITH_MD5,
151         SSL2_CK_RC4_64_WITH_MD5,
152         SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_SSLV2|SSL_LOW,
153         SSL2_CF_8_BYTE_ENC,
154         SSL_ALL_CIPHERS,
155         },
156 #endif
157 /* NULL SSLeay (testing) */
158 #if 0
159         {       
160         0,
161         SSL2_TXT_NULL,
162         SSL2_CK_NULL,
163         0,
164         SSL_ALL_CIPHERS,
165         },
166 #endif
167
168 /* end of list :-) */
169         };
170
171 static SSL_METHOD SSLv2_data= {
172         SSL2_VERSION,
173         ssl2_new,       /* local */
174         ssl2_clear,     /* local */
175         ssl2_free,      /* local */
176         ssl_undefined_function,
177         ssl_undefined_function,
178         ssl2_read,
179         ssl2_peek,
180         ssl2_write,
181         ssl2_shutdown,
182         ssl_ok, /* NULL - renegotiate */
183         ssl_ok, /* NULL - check renegotiate */
184         ssl2_ctrl,      /* local */
185         ssl2_ctx_ctrl,  /* local */
186         ssl2_get_cipher_by_char,
187         ssl2_put_cipher_by_char,
188         ssl2_pending,
189         ssl2_num_ciphers,
190         ssl2_get_cipher,
191         ssl_bad_method,
192         ssl2_default_timeout,
193         &ssl3_undef_enc_method,
194         };
195
196 static long ssl2_default_timeout(void)
197         {
198         return(300);
199         }
200
201 SSL_METHOD *sslv2_base_method(void)
202         {
203         return(&SSLv2_data);
204         }
205
206 int ssl2_num_ciphers(void)
207         {
208         return(SSL2_NUM_CIPHERS);
209         }
210
211 SSL_CIPHER *ssl2_get_cipher(unsigned int u)
212         {
213         if (u < SSL2_NUM_CIPHERS)
214                 return(&(ssl2_ciphers[SSL2_NUM_CIPHERS-1-u]));
215         else
216                 return(NULL);
217         }
218
219 int ssl2_pending(SSL *s)
220         {
221         return(s->s2->ract_data_length);
222         }
223
224 int ssl2_new(SSL *s)
225         {
226         SSL2_CTX *s2;
227
228         if ((s2=(SSL2_CTX *)Malloc(sizeof(SSL2_CTX))) == NULL) goto err;
229         memset(s2,0,sizeof(SSL2_CTX));
230
231         if ((s2->rbuf=(unsigned char *)Malloc(
232                 SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2)) == NULL) goto err;
233         if ((s2->wbuf=(unsigned char *)Malloc(
234                 SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2)) == NULL) goto err;
235         s->s2=s2;
236
237         ssl2_clear(s);
238         return(1);
239 err:
240         if (s2 != NULL)
241                 {
242                 if (s2->wbuf != NULL) Free(s2->wbuf);
243                 if (s2->rbuf != NULL) Free(s2->rbuf);
244                 Free(s2);
245                 }
246         return(0);
247         }
248
249 void ssl2_free(SSL *s)
250         {
251         SSL2_CTX *s2;
252
253         if(s == NULL)
254             return;
255
256         s2=s->s2;
257         if (s2->rbuf != NULL) Free(s2->rbuf);
258         if (s2->wbuf != NULL) Free(s2->wbuf);
259         memset(s2,0,sizeof(SSL2_CTX));
260         Free(s2);
261         s->s2=NULL;
262         }
263
264 void ssl2_clear(SSL *s)
265         {
266         SSL2_CTX *s2;
267         unsigned char *rbuf,*wbuf;
268
269         s2=s->s2;
270
271         rbuf=s2->rbuf;
272         wbuf=s2->wbuf;
273
274         memset(s2,0,sizeof(SSL2_CTX));
275
276         s2->rbuf=rbuf;
277         s2->wbuf=wbuf;
278         s2->clear_text=1;
279         s->packet=s2->rbuf;
280         s->version=SSL2_VERSION;
281         s->packet_length=0;
282         }
283
284 long ssl2_ctrl(SSL *s, int cmd, long larg, char *parg)
285         {
286         int ret=0;
287
288         switch(cmd)
289                 {
290         case SSL_CTRL_GET_SESSION_REUSED:
291                 ret=s->hit;
292                 break;
293         default:
294                 break;
295                 }
296         return(ret);
297         }
298
299 long ssl2_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, char *parg)
300         {
301         return(0);
302         }
303
304 /* This function needs to check if the ciphers required are actually
305  * available */
306 SSL_CIPHER *ssl2_get_cipher_by_char(const unsigned char *p)
307         {
308         static int init=1;
309         static SSL_CIPHER *sorted[SSL2_NUM_CIPHERS];
310         SSL_CIPHER c,*cp= &c,**cpp;
311         unsigned long id;
312         int i;
313
314         if (init)
315                 {
316                 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
317
318                 for (i=0; i<SSL2_NUM_CIPHERS; i++)
319                         sorted[i]= &(ssl2_ciphers[i]);
320
321                 qsort(  (char *)sorted,
322                         SSL2_NUM_CIPHERS,sizeof(SSL_CIPHER *),
323                         FP_ICC ssl_cipher_ptr_id_cmp);
324
325                 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
326                 init=0;
327                 }
328
329         id=0x02000000L|((unsigned long)p[0]<<16L)|
330                 ((unsigned long)p[1]<<8L)|(unsigned long)p[2];
331         c.id=id;
332         cpp=(SSL_CIPHER **)OBJ_bsearch((char *)&cp,
333                 (char *)sorted,
334                 SSL2_NUM_CIPHERS,sizeof(SSL_CIPHER *),
335                 (int (*)())ssl_cipher_ptr_id_cmp);
336         if ((cpp == NULL) || !(*cpp)->valid)
337                 return(NULL);
338         else
339                 return(*cpp);
340         }
341
342 int ssl2_put_cipher_by_char(const SSL_CIPHER *c, unsigned char *p)
343         {
344         long l;
345
346         if (p != NULL)
347                 {
348                 l=c->id;
349                 if ((l & 0xff000000) != 0x02000000) return(0);
350                 p[0]=((unsigned char)(l>>16L))&0xFF;
351                 p[1]=((unsigned char)(l>> 8L))&0xFF;
352                 p[2]=((unsigned char)(l     ))&0xFF;
353                 }
354         return(3);
355         }
356
357 void ssl2_generate_key_material(SSL *s)
358         {
359         unsigned int i;
360         MD5_CTX ctx;
361         unsigned char *km;
362         unsigned char c='0';
363
364 #ifdef CHARSET_EBCDIC
365         c = os_toascii['0']; /* Must be an ASCII '0', not EBCDIC '0',
366                                 see SSLv2 docu */
367 #endif
368
369         km=s->s2->key_material;
370         for (i=0; i<s->s2->key_material_length; i+=MD5_DIGEST_LENGTH)
371                 {
372                 MD5_Init(&ctx);
373
374                 MD5_Update(&ctx,s->session->master_key,s->session->master_key_length);
375                 MD5_Update(&ctx,(unsigned char *)&c,1);
376                 c++;
377                 MD5_Update(&ctx,s->s2->challenge,s->s2->challenge_length);
378                 MD5_Update(&ctx,s->s2->conn_id,s->s2->conn_id_length);
379                 MD5_Final(km,&ctx);
380                 km+=MD5_DIGEST_LENGTH;
381                 }
382         }
383
384 void ssl2_return_error(SSL *s, int err)
385         {
386         if (!s->error)
387                 {
388                 s->error=3;
389                 s->error_code=err;
390
391                 ssl2_write_error(s);
392                 }
393         }
394
395
396 void ssl2_write_error(SSL *s)
397         {
398         unsigned char buf[3];
399         int i,error;
400
401         buf[0]=SSL2_MT_ERROR;
402         buf[1]=(s->error_code>>8)&0xff;
403         buf[2]=(s->error_code)&0xff;
404
405 /*      state=s->rwstate;*/
406         error=s->error;
407         s->error=0;
408         i=ssl2_write(s,&(buf[3-error]),error);
409 /*      if (i == error) s->rwstate=state; */
410
411         if (i < 0)
412                 s->error=error;
413         else if (i != s->error)
414                 s->error=error-i;
415         /* else
416                 s->error=0; */
417         }
418
419 int ssl2_shutdown(SSL *s)
420         {
421         s->shutdown=(SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
422         return(1);
423         }
424 #endif