Synchronise more with the Unix build.
[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 #include "ssl_locl.h"
60 #ifndef OPENSSL_NO_SSL2
61 #include <stdio.h>
62 #include <openssl/objects.h>
63 #include <openssl/evp.h>
64 #include <openssl/md5.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_SSLV2,
79         SSL_EXPORT|SSL_EXP40|SSL_STRONG_NONE,
80         0,
81         0,
82         0,
83         SSL_ALL_CIPHERS,
84         SSL_ALL_STRENGTHS,
85         },
86 #endif
87 /* RC4_128_EXPORT40_WITH_MD5 */
88         {
89         1,
90         SSL2_TXT_RC4_128_EXPORT40_WITH_MD5,
91         SSL2_CK_RC4_128_EXPORT40_WITH_MD5,
92         SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_SSLV2,
93         SSL_EXPORT|SSL_EXP40,
94         SSL2_CF_5_BYTE_ENC,
95         40,
96         128,
97         SSL_ALL_CIPHERS,
98         SSL_ALL_STRENGTHS,
99         },
100 /* RC4_128_WITH_MD5 */
101         {
102         1,
103         SSL2_TXT_RC4_128_WITH_MD5,
104         SSL2_CK_RC4_128_WITH_MD5,
105         SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_SSLV2,
106         SSL_NOT_EXP|SSL_MEDIUM,
107         0,
108         128,
109         128,
110         SSL_ALL_CIPHERS,
111         SSL_ALL_STRENGTHS,
112         },
113 /* RC2_128_CBC_EXPORT40_WITH_MD5 */
114         {
115         1,
116         SSL2_TXT_RC2_128_CBC_EXPORT40_WITH_MD5,
117         SSL2_CK_RC2_128_CBC_EXPORT40_WITH_MD5,
118         SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5|SSL_SSLV2,
119         SSL_EXPORT|SSL_EXP40,
120         SSL2_CF_5_BYTE_ENC,
121         40,
122         128,
123         SSL_ALL_CIPHERS,
124         SSL_ALL_STRENGTHS,
125         },
126 /* RC2_128_CBC_WITH_MD5 */
127         {
128         1,
129         SSL2_TXT_RC2_128_CBC_WITH_MD5,
130         SSL2_CK_RC2_128_CBC_WITH_MD5,
131         SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5|SSL_SSLV2,
132         SSL_NOT_EXP|SSL_MEDIUM,
133         0,
134         128,
135         128,
136         SSL_ALL_CIPHERS,
137         SSL_ALL_STRENGTHS,
138         },
139 /* IDEA_128_CBC_WITH_MD5 */
140 #ifndef OPENSSL_NO_IDEA
141         {
142         1,
143         SSL2_TXT_IDEA_128_CBC_WITH_MD5,
144         SSL2_CK_IDEA_128_CBC_WITH_MD5,
145         SSL_kRSA|SSL_aRSA|SSL_IDEA|SSL_MD5|SSL_SSLV2,
146         SSL_NOT_EXP|SSL_MEDIUM,
147         0,
148         128,
149         128,
150         SSL_ALL_CIPHERS,
151         SSL_ALL_STRENGTHS,
152         },
153 #endif
154 /* DES_64_CBC_WITH_MD5 */
155         {
156         1,
157         SSL2_TXT_DES_64_CBC_WITH_MD5,
158         SSL2_CK_DES_64_CBC_WITH_MD5,
159         SSL_kRSA|SSL_aRSA|SSL_DES|SSL_MD5|SSL_SSLV2,
160         SSL_NOT_EXP|SSL_LOW,
161         0,
162         56,
163         56,
164         SSL_ALL_CIPHERS,
165         SSL_ALL_STRENGTHS,
166         },
167 /* DES_192_EDE3_CBC_WITH_MD5 */
168         {
169         1,
170         SSL2_TXT_DES_192_EDE3_CBC_WITH_MD5,
171         SSL2_CK_DES_192_EDE3_CBC_WITH_MD5,
172         SSL_kRSA|SSL_aRSA|SSL_3DES|SSL_MD5|SSL_SSLV2,
173         SSL_NOT_EXP|SSL_HIGH,
174         0,
175         168,
176         168,
177         SSL_ALL_CIPHERS,
178         SSL_ALL_STRENGTHS,
179         },
180 /* RC4_64_WITH_MD5 */
181 #if 1
182         {
183         1,
184         SSL2_TXT_RC4_64_WITH_MD5,
185         SSL2_CK_RC4_64_WITH_MD5,
186         SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_SSLV2,
187         SSL_NOT_EXP|SSL_LOW,
188         SSL2_CF_8_BYTE_ENC,
189         64,
190         64,
191         SSL_ALL_CIPHERS,
192         SSL_ALL_STRENGTHS,
193         },
194 #endif
195 /* NULL SSLeay (testing) */
196 #if 0
197         {       
198         0,
199         SSL2_TXT_NULL,
200         SSL2_CK_NULL,
201         0,
202         SSL_STRONG_NONE,
203         0,
204         0,
205         0,
206         SSL_ALL_CIPHERS,
207         SSL_ALL_STRENGTHS,
208         },
209 #endif
210
211 /* end of list :-) */
212         };
213
214 static SSL_METHOD SSLv2_data= {
215         SSL2_VERSION,
216         ssl2_new,       /* local */
217         ssl2_clear,     /* local */
218         ssl2_free,      /* local */
219         ssl_undefined_function,
220         ssl_undefined_function,
221         ssl2_read,
222         ssl2_peek,
223         ssl2_write,
224         ssl2_shutdown,
225         ssl_ok, /* NULL - renegotiate */
226         ssl_ok, /* NULL - check renegotiate */
227         NULL, /* NULL - ssl_get_message */
228         NULL, /* NULL - ssl_get_record */
229         NULL, /* NULL - ssl_write_bytes */
230         NULL, /* NULL - dispatch_alert */
231         ssl2_ctrl,      /* local */
232         ssl2_ctx_ctrl,  /* local */
233         ssl2_get_cipher_by_char,
234         ssl2_put_cipher_by_char,
235         ssl2_pending,
236         ssl2_num_ciphers,
237         ssl2_get_cipher,
238         ssl_bad_method,
239         ssl2_default_timeout,
240         &ssl3_undef_enc_method,
241         ssl_undefined_void_function,
242         ssl2_callback_ctrl,     /* local */
243         ssl2_ctx_callback_ctrl, /* local */
244         };
245
246 static long ssl2_default_timeout(void)
247         {
248         return(300);
249         }
250
251 SSL_METHOD *sslv2_base_method(void)
252         {
253         return(&SSLv2_data);
254         }
255
256 int ssl2_num_ciphers(void)
257         {
258         return(SSL2_NUM_CIPHERS);
259         }
260
261 SSL_CIPHER *ssl2_get_cipher(unsigned int u)
262         {
263         if (u < SSL2_NUM_CIPHERS)
264                 return(&(ssl2_ciphers[SSL2_NUM_CIPHERS-1-u]));
265         else
266                 return(NULL);
267         }
268
269 int ssl2_pending(const SSL *s)
270         {
271         return SSL_in_init(s) ? 0 : s->s2->ract_data_length;
272         }
273
274 int ssl2_new(SSL *s)
275         {
276         SSL2_STATE *s2;
277
278         if ((s2=OPENSSL_malloc(sizeof *s2)) == NULL) goto err;
279         memset(s2,0,sizeof *s2);
280
281 #if SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER + 3 > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER + 2
282 #  error "assertion failed"
283 #endif
284
285         if ((s2->rbuf=OPENSSL_malloc(
286                 SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2)) == NULL) goto err;
287         /* wbuf needs one byte more because when using two-byte headers,
288          * we leave the first byte unused in do_ssl_write (s2_pkt.c) */
289         if ((s2->wbuf=OPENSSL_malloc(
290                 SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+3)) == NULL) goto err;
291         s->s2=s2;
292
293         ssl2_clear(s);
294         return(1);
295 err:
296         if (s2 != NULL)
297                 {
298                 if (s2->wbuf != NULL) OPENSSL_free(s2->wbuf);
299                 if (s2->rbuf != NULL) OPENSSL_free(s2->rbuf);
300                 OPENSSL_free(s2);
301                 }
302         return(0);
303         }
304
305 void ssl2_free(SSL *s)
306         {
307         SSL2_STATE *s2;
308
309         if(s == NULL)
310             return;
311
312         s2=s->s2;
313         if (s2->rbuf != NULL) OPENSSL_free(s2->rbuf);
314         if (s2->wbuf != NULL) OPENSSL_free(s2->wbuf);
315         OPENSSL_cleanse(s2,sizeof *s2);
316         OPENSSL_free(s2);
317         s->s2=NULL;
318         }
319
320 void ssl2_clear(SSL *s)
321         {
322         SSL2_STATE *s2;
323         unsigned char *rbuf,*wbuf;
324
325         s2=s->s2;
326
327         rbuf=s2->rbuf;
328         wbuf=s2->wbuf;
329
330         memset(s2,0,sizeof *s2);
331
332         s2->rbuf=rbuf;
333         s2->wbuf=wbuf;
334         s2->clear_text=1;
335         s->packet=s2->rbuf;
336         s->version=SSL2_VERSION;
337         s->packet_length=0;
338         }
339
340 long ssl2_ctrl(SSL *s, int cmd, long larg, void *parg)
341         {
342         int ret=0;
343
344         switch(cmd)
345                 {
346         case SSL_CTRL_GET_SESSION_REUSED:
347                 ret=s->hit;
348                 break;
349         default:
350                 break;
351                 }
352         return(ret);
353         }
354
355 long ssl2_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
356         {
357         return(0);
358         }
359
360 long ssl2_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
361         {
362         return(0);
363         }
364
365 long ssl2_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
366         {
367         return(0);
368         }
369
370 /* This function needs to check if the ciphers required are actually
371  * available */
372 SSL_CIPHER *ssl2_get_cipher_by_char(const unsigned char *p)
373         {
374         static int init=1;
375         static SSL_CIPHER *sorted[SSL2_NUM_CIPHERS];
376         SSL_CIPHER c,*cp= &c,**cpp;
377         unsigned long id;
378         unsigned int i;
379
380         if (init)
381                 {
382                 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
383
384                 if (init)
385                         {
386                         for (i=0; i<SSL2_NUM_CIPHERS; i++)
387                                 sorted[i]= &(ssl2_ciphers[i]);
388
389                         qsort((char *)sorted,
390                                 SSL2_NUM_CIPHERS,sizeof(SSL_CIPHER *),
391                                 FP_ICC ssl_cipher_ptr_id_cmp);
392
393                         init=0;
394                         }
395                         
396                 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
397                 }
398
399         id=0x02000000L|((unsigned long)p[0]<<16L)|
400                 ((unsigned long)p[1]<<8L)|(unsigned long)p[2];
401         c.id=id;
402         cpp=(SSL_CIPHER **)OBJ_bsearch((char *)&cp,
403                 (char *)sorted,
404                 SSL2_NUM_CIPHERS,sizeof(SSL_CIPHER *),
405                 FP_ICC ssl_cipher_ptr_id_cmp);
406         if ((cpp == NULL) || !(*cpp)->valid)
407                 return(NULL);
408         else
409                 return(*cpp);
410         }
411
412 int ssl2_put_cipher_by_char(const SSL_CIPHER *c, unsigned char *p)
413         {
414         long l;
415
416         if (p != NULL)
417                 {
418                 l=c->id;
419                 if ((l & 0xff000000) != 0x02000000) return(0);
420                 p[0]=((unsigned char)(l>>16L))&0xFF;
421                 p[1]=((unsigned char)(l>> 8L))&0xFF;
422                 p[2]=((unsigned char)(l     ))&0xFF;
423                 }
424         return(3);
425         }
426
427 int ssl2_generate_key_material(SSL *s)
428         {
429         unsigned int i;
430         EVP_MD_CTX ctx;
431         unsigned char *km;
432         unsigned char c='0';
433         const EVP_MD *md5;
434
435         md5 = EVP_md5();
436
437 #ifdef CHARSET_EBCDIC
438         c = os_toascii['0']; /* Must be an ASCII '0', not EBCDIC '0',
439                                 see SSLv2 docu */
440 #endif
441         EVP_MD_CTX_init(&ctx);
442         km=s->s2->key_material;
443
444         if (s->session->master_key_length < 0 ||
445                         s->session->master_key_length > (int)sizeof(s->session->master_key))
446                 {
447                 SSLerr(SSL_F_SSL2_GENERATE_KEY_MATERIAL, ERR_R_INTERNAL_ERROR);
448                 return 0;
449                 }
450
451         for (i=0; i<s->s2->key_material_length; i += EVP_MD_size(md5))
452                 {
453                 if (((km - s->s2->key_material) + EVP_MD_size(md5)) >
454                                 (int)sizeof(s->s2->key_material))
455                         {
456                         /* EVP_DigestFinal_ex() below would write beyond buffer */
457                         SSLerr(SSL_F_SSL2_GENERATE_KEY_MATERIAL, ERR_R_INTERNAL_ERROR);
458                         return 0;
459                         }
460
461                 EVP_DigestInit_ex(&ctx, md5, NULL);
462
463                 OPENSSL_assert(s->session->master_key_length >= 0
464                     && s->session->master_key_length
465                     < (int)sizeof(s->session->master_key));
466                 EVP_DigestUpdate(&ctx,s->session->master_key,s->session->master_key_length);
467                 EVP_DigestUpdate(&ctx,&c,1);
468                 c++;
469                 EVP_DigestUpdate(&ctx,s->s2->challenge,s->s2->challenge_length);
470                 EVP_DigestUpdate(&ctx,s->s2->conn_id,s->s2->conn_id_length);
471                 EVP_DigestFinal_ex(&ctx,km,NULL);
472                 km += EVP_MD_size(md5);
473                 }
474
475         EVP_MD_CTX_cleanup(&ctx);
476         return 1;
477         }
478
479 void ssl2_return_error(SSL *s, int err)
480         {
481         if (!s->error)
482                 {
483                 s->error=3;
484                 s->error_code=err;
485
486                 ssl2_write_error(s);
487                 }
488         }
489
490
491 void ssl2_write_error(SSL *s)
492         {
493         unsigned char buf[3];
494         int i,error;
495
496         buf[0]=SSL2_MT_ERROR;
497         buf[1]=(s->error_code>>8)&0xff;
498         buf[2]=(s->error_code)&0xff;
499
500 /*      state=s->rwstate;*/
501
502         error=s->error; /* number of bytes left to write */
503         s->error=0;
504         OPENSSL_assert(error >= 0 && error <= (int)sizeof(buf));
505         i=ssl2_write(s,&(buf[3-error]),error);
506
507 /*      if (i == error) s->rwstate=state; */
508
509         if (i < 0)
510                 s->error=error;
511         else
512                 {
513                 s->error=error-i;
514
515                 if (s->error == 0)
516                         if (s->msg_callback)
517                                 s->msg_callback(1, s->version, 0, buf, 3, s, s->msg_callback_arg); /* ERROR */
518                 }
519         }
520
521 int ssl2_shutdown(SSL *s)
522         {
523         s->shutdown=(SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
524         return(1);
525         }
526 #else /* !OPENSSL_NO_SSL2 */
527
528 # if PEDANTIC
529 static void *dummy=&dummy;
530 # endif
531
532 #endif