7c71f5e3211f6dea1d6829ca5776f29277cd5362
[openssl.git] / ssl / s3_lib.c
1 /* ssl/s3_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 <stdio.h>
60 #include <openssl/md5.h>
61 #include <openssl/sha.h>
62 #include <openssl/objects.h>
63 #include "ssl_locl.h"
64
65 const char *ssl3_version_str="SSLv3" OPENSSL_VERSION_PTEXT;
66
67 #define SSL3_NUM_CIPHERS        (sizeof(ssl3_ciphers)/sizeof(SSL_CIPHER))
68
69 static long ssl3_default_timeout(void );
70
71 OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
72 /* The RSA ciphers */
73 /* Cipher 01 */
74         {
75         1,
76         SSL3_TXT_RSA_NULL_MD5,
77         SSL3_CK_RSA_NULL_MD5,
78         SSL_kRSA|SSL_aRSA|SSL_eNULL |SSL_MD5|SSL_SSLV3,
79         SSL_NOT_EXP,
80         0,
81         0,
82         0,
83         SSL_ALL_CIPHERS,
84         SSL_ALL_STRENGTHS,
85         },
86 /* Cipher 02 */
87         {
88         1,
89         SSL3_TXT_RSA_NULL_SHA,
90         SSL3_CK_RSA_NULL_SHA,
91         SSL_kRSA|SSL_aRSA|SSL_eNULL |SSL_SHA1|SSL_SSLV3,
92         SSL_NOT_EXP,
93         0,
94         0,
95         0,
96         SSL_ALL_CIPHERS,
97         SSL_ALL_STRENGTHS,
98         },
99
100 /* anon DH */
101 /* Cipher 17 */
102         {
103         1,
104         SSL3_TXT_ADH_RC4_40_MD5,
105         SSL3_CK_ADH_RC4_40_MD5,
106         SSL_kEDH |SSL_aNULL|SSL_RC4  |SSL_MD5 |SSL_SSLV3,
107         SSL_EXPORT|SSL_EXP40,
108         0,
109         40,
110         128,
111         SSL_ALL_CIPHERS,
112         SSL_ALL_STRENGTHS,
113         },
114 /* Cipher 18 */
115         {
116         1,
117         SSL3_TXT_ADH_RC4_128_MD5,
118         SSL3_CK_ADH_RC4_128_MD5,
119         SSL_kEDH |SSL_aNULL|SSL_RC4  |SSL_MD5 |SSL_SSLV3,
120         SSL_NOT_EXP,
121         0,
122         128,
123         128,
124         SSL_ALL_CIPHERS,
125         SSL_ALL_STRENGTHS,
126         },
127 /* Cipher 19 */
128         {
129         1,
130         SSL3_TXT_ADH_DES_40_CBC_SHA,
131         SSL3_CK_ADH_DES_40_CBC_SHA,
132         SSL_kEDH |SSL_aNULL|SSL_DES|SSL_SHA1|SSL_SSLV3,
133         SSL_EXPORT|SSL_EXP40,
134         0,
135         40,
136         128,
137         SSL_ALL_CIPHERS,
138         SSL_ALL_STRENGTHS,
139         },
140 /* Cipher 1A */
141         {
142         1,
143         SSL3_TXT_ADH_DES_64_CBC_SHA,
144         SSL3_CK_ADH_DES_64_CBC_SHA,
145         SSL_kEDH |SSL_aNULL|SSL_DES  |SSL_SHA1|SSL_SSLV3,
146         SSL_NOT_EXP,
147         0,
148         56,
149         56,
150         SSL_ALL_CIPHERS,
151         SSL_ALL_STRENGTHS,
152         },
153 /* Cipher 1B */
154         {
155         1,
156         SSL3_TXT_ADH_DES_192_CBC_SHA,
157         SSL3_CK_ADH_DES_192_CBC_SHA,
158         SSL_kEDH |SSL_aNULL|SSL_3DES |SSL_SHA1|SSL_SSLV3,
159         SSL_NOT_EXP,
160         0,
161         168,
162         168,
163         SSL_ALL_CIPHERS,
164         SSL_ALL_STRENGTHS,
165         },
166
167 /* RSA again */
168 /* Cipher 03 */
169         {
170         1,
171         SSL3_TXT_RSA_RC4_40_MD5,
172         SSL3_CK_RSA_RC4_40_MD5,
173         SSL_kRSA|SSL_aRSA|SSL_RC4  |SSL_MD5 |SSL_SSLV3,
174         SSL_EXPORT|SSL_EXP40,
175         0,
176         40,
177         128,
178         SSL_ALL_CIPHERS,
179         SSL_ALL_STRENGTHS,
180         },
181 /* Cipher 04 */
182         {
183         1,
184         SSL3_TXT_RSA_RC4_128_MD5,
185         SSL3_CK_RSA_RC4_128_MD5,
186         SSL_kRSA|SSL_aRSA|SSL_RC4  |SSL_MD5|SSL_SSLV3,
187         SSL_NOT_EXP|SSL_MEDIUM,
188         0,
189         128,
190         128,
191         SSL_ALL_CIPHERS,
192         SSL_ALL_STRENGTHS,
193         },
194 /* Cipher 05 */
195         {
196         1,
197         SSL3_TXT_RSA_RC4_128_SHA,
198         SSL3_CK_RSA_RC4_128_SHA,
199         SSL_kRSA|SSL_aRSA|SSL_RC4  |SSL_SHA1|SSL_SSLV3,
200         SSL_NOT_EXP|SSL_MEDIUM,
201         0,
202         128,
203         128,
204         SSL_ALL_CIPHERS,
205         SSL_ALL_STRENGTHS,
206         },
207 /* Cipher 06 */
208         {
209         1,
210         SSL3_TXT_RSA_RC2_40_MD5,
211         SSL3_CK_RSA_RC2_40_MD5,
212         SSL_kRSA|SSL_aRSA|SSL_RC2  |SSL_MD5 |SSL_SSLV3,
213         SSL_EXPORT|SSL_EXP40,
214         0,
215         40,
216         128,
217         SSL_ALL_CIPHERS,
218         SSL_ALL_STRENGTHS,
219         },
220 /* Cipher 07 */
221         {
222         1,
223         SSL3_TXT_RSA_IDEA_128_SHA,
224         SSL3_CK_RSA_IDEA_128_SHA,
225         SSL_kRSA|SSL_aRSA|SSL_IDEA |SSL_SHA1|SSL_SSLV3,
226         SSL_NOT_EXP|SSL_MEDIUM,
227         0,
228         128,
229         128,
230         SSL_ALL_CIPHERS,
231         SSL_ALL_STRENGTHS,
232         },
233 /* Cipher 08 */
234         {
235         1,
236         SSL3_TXT_RSA_DES_40_CBC_SHA,
237         SSL3_CK_RSA_DES_40_CBC_SHA,
238         SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA1|SSL_SSLV3,
239         SSL_EXPORT|SSL_EXP40,
240         0,
241         40,
242         56,
243         SSL_ALL_CIPHERS,
244         SSL_ALL_STRENGTHS,
245         },
246 /* Cipher 09 */
247         {
248         1,
249         SSL3_TXT_RSA_DES_64_CBC_SHA,
250         SSL3_CK_RSA_DES_64_CBC_SHA,
251         SSL_kRSA|SSL_aRSA|SSL_DES  |SSL_SHA1|SSL_SSLV3,
252         SSL_NOT_EXP|SSL_LOW,
253         0,
254         56,
255         56,
256         SSL_ALL_CIPHERS,
257         SSL_ALL_STRENGTHS,
258         },
259 /* Cipher 0A */
260         {
261         1,
262         SSL3_TXT_RSA_DES_192_CBC3_SHA,
263         SSL3_CK_RSA_DES_192_CBC3_SHA,
264         SSL_kRSA|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_SSLV3,
265         SSL_NOT_EXP|SSL_HIGH,
266         0,
267         168,
268         168,
269         SSL_ALL_CIPHERS,
270         SSL_ALL_STRENGTHS,
271         },
272
273 /*  The DH ciphers */
274 /* Cipher 0B */
275         {
276         0,
277         SSL3_TXT_DH_DSS_DES_40_CBC_SHA,
278         SSL3_CK_DH_DSS_DES_40_CBC_SHA,
279         SSL_kDHd |SSL_aDH|SSL_DES|SSL_SHA1|SSL_SSLV3,
280         SSL_EXPORT|SSL_EXP40,
281         0,
282         40,
283         56,
284         SSL_ALL_CIPHERS,
285         SSL_ALL_STRENGTHS,
286         },
287 /* Cipher 0C */
288         {
289         0,
290         SSL3_TXT_DH_DSS_DES_64_CBC_SHA,
291         SSL3_CK_DH_DSS_DES_64_CBC_SHA,
292         SSL_kDHd |SSL_aDH|SSL_DES  |SSL_SHA1|SSL_SSLV3,
293         SSL_NOT_EXP|SSL_LOW,
294         0,
295         56,
296         56,
297         SSL_ALL_CIPHERS,
298         SSL_ALL_STRENGTHS,
299         },
300 /* Cipher 0D */
301         {
302         0,
303         SSL3_TXT_DH_DSS_DES_192_CBC3_SHA,
304         SSL3_CK_DH_DSS_DES_192_CBC3_SHA,
305         SSL_kDHd |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_SSLV3,
306         SSL_NOT_EXP|SSL_HIGH,
307         0,
308         168,
309         168,
310         SSL_ALL_CIPHERS,
311         SSL_ALL_STRENGTHS,
312         },
313 /* Cipher 0E */
314         {
315         0,
316         SSL3_TXT_DH_RSA_DES_40_CBC_SHA,
317         SSL3_CK_DH_RSA_DES_40_CBC_SHA,
318         SSL_kDHr |SSL_aDH|SSL_DES|SSL_SHA1|SSL_SSLV3,
319         SSL_EXPORT|SSL_EXP40,
320         0,
321         40,
322         56,
323         SSL_ALL_CIPHERS,
324         SSL_ALL_STRENGTHS,
325         },
326 /* Cipher 0F */
327         {
328         0,
329         SSL3_TXT_DH_RSA_DES_64_CBC_SHA,
330         SSL3_CK_DH_RSA_DES_64_CBC_SHA,
331         SSL_kDHr |SSL_aDH|SSL_DES  |SSL_SHA1|SSL_SSLV3,
332         SSL_NOT_EXP|SSL_LOW,
333         0,
334         56,
335         56,
336         SSL_ALL_CIPHERS,
337         SSL_ALL_STRENGTHS,
338         },
339 /* Cipher 10 */
340         {
341         0,
342         SSL3_TXT_DH_RSA_DES_192_CBC3_SHA,
343         SSL3_CK_DH_RSA_DES_192_CBC3_SHA,
344         SSL_kDHr |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_SSLV3,
345         SSL_NOT_EXP|SSL_HIGH,
346         0,
347         168,
348         168,
349         SSL_ALL_CIPHERS,
350         SSL_ALL_STRENGTHS,
351         },
352
353 /* The Ephemeral DH ciphers */
354 /* Cipher 11 */
355         {
356         1,
357         SSL3_TXT_EDH_DSS_DES_40_CBC_SHA,
358         SSL3_CK_EDH_DSS_DES_40_CBC_SHA,
359         SSL_kEDH|SSL_aDSS|SSL_DES|SSL_SHA1|SSL_SSLV3,
360         SSL_EXPORT|SSL_EXP40,
361         0,
362         40,
363         56,
364         SSL_ALL_CIPHERS,
365         SSL_ALL_STRENGTHS,
366         },
367 /* Cipher 12 */
368         {
369         1,
370         SSL3_TXT_EDH_DSS_DES_64_CBC_SHA,
371         SSL3_CK_EDH_DSS_DES_64_CBC_SHA,
372         SSL_kEDH|SSL_aDSS|SSL_DES  |SSL_SHA1|SSL_SSLV3,
373         SSL_NOT_EXP|SSL_LOW,
374         0,
375         56,
376         56,
377         SSL_ALL_CIPHERS,
378         SSL_ALL_STRENGTHS,
379         },
380 /* Cipher 13 */
381         {
382         1,
383         SSL3_TXT_EDH_DSS_DES_192_CBC3_SHA,
384         SSL3_CK_EDH_DSS_DES_192_CBC3_SHA,
385         SSL_kEDH|SSL_aDSS|SSL_3DES |SSL_SHA1|SSL_SSLV3,
386         SSL_NOT_EXP|SSL_HIGH,
387         0,
388         168,
389         168,
390         SSL_ALL_CIPHERS,
391         SSL_ALL_STRENGTHS,
392         },
393 /* Cipher 14 */
394         {
395         1,
396         SSL3_TXT_EDH_RSA_DES_40_CBC_SHA,
397         SSL3_CK_EDH_RSA_DES_40_CBC_SHA,
398         SSL_kEDH|SSL_aRSA|SSL_DES|SSL_SHA1|SSL_SSLV3,
399         SSL_EXPORT|SSL_EXP40,
400         0,
401         40,
402         56,
403         SSL_ALL_CIPHERS,
404         SSL_ALL_STRENGTHS,
405         },
406 /* Cipher 15 */
407         {
408         1,
409         SSL3_TXT_EDH_RSA_DES_64_CBC_SHA,
410         SSL3_CK_EDH_RSA_DES_64_CBC_SHA,
411         SSL_kEDH|SSL_aRSA|SSL_DES  |SSL_SHA1|SSL_SSLV3,
412         SSL_NOT_EXP|SSL_LOW,
413         0,
414         56,
415         56,
416         SSL_ALL_CIPHERS,
417         SSL_ALL_STRENGTHS,
418         },
419 /* Cipher 16 */
420         {
421         1,
422         SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA,
423         SSL3_CK_EDH_RSA_DES_192_CBC3_SHA,
424         SSL_kEDH|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_SSLV3,
425         SSL_NOT_EXP|SSL_HIGH,
426         0,
427         168,
428         168,
429         SSL_ALL_CIPHERS,
430         SSL_ALL_STRENGTHS,
431         },
432
433 /* Fortezza */
434 /* Cipher 1C */
435         {
436         0,
437         SSL3_TXT_FZA_DMS_NULL_SHA,
438         SSL3_CK_FZA_DMS_NULL_SHA,
439         SSL_kFZA|SSL_aFZA |SSL_eNULL |SSL_SHA1|SSL_SSLV3,
440         SSL_NOT_EXP,
441         0,
442         0,
443         0,
444         SSL_ALL_CIPHERS,
445         SSL_ALL_STRENGTHS,
446         },
447
448 /* Cipher 1D */
449         {
450         0,
451         SSL3_TXT_FZA_DMS_FZA_SHA,
452         SSL3_CK_FZA_DMS_FZA_SHA,
453         SSL_kFZA|SSL_aFZA |SSL_eFZA |SSL_SHA1|SSL_SSLV3,
454         SSL_NOT_EXP,
455         0,
456         0,
457         0,
458         SSL_ALL_CIPHERS,
459         SSL_ALL_STRENGTHS,
460         },
461
462 /* Cipher 1E */
463         {
464         0,
465         SSL3_TXT_FZA_DMS_RC4_SHA,
466         SSL3_CK_FZA_DMS_RC4_SHA,
467         SSL_kFZA|SSL_aFZA |SSL_RC4  |SSL_SHA1|SSL_SSLV3,
468         SSL_NOT_EXP,
469         0,
470         128,
471         128,
472         SSL_ALL_CIPHERS,
473         SSL_ALL_STRENGTHS,
474         },
475
476 #if TLS1_ALLOW_EXPERIMENTAL_CIPHERSUITES
477         /* New TLS Export CipherSuites */
478         /* Cipher 60 */
479             {
480             1,
481             TLS1_TXT_RSA_EXPORT1024_WITH_RC4_56_MD5,
482             TLS1_CK_RSA_EXPORT1024_WITH_RC4_56_MD5,
483             SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_TLSV1,
484             SSL_EXPORT|SSL_EXP56,
485             0,
486             56,
487             128,
488             SSL_ALL_CIPHERS,
489             SSL_ALL_STRENGTHS,
490             },
491         /* Cipher 61 */
492             {
493             1,
494             TLS1_TXT_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5,
495             TLS1_CK_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5,
496             SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5|SSL_TLSV1,
497             SSL_EXPORT|SSL_EXP56,
498             0,
499             56,
500             128,
501             SSL_ALL_CIPHERS,
502             SSL_ALL_STRENGTHS,
503             },
504         /* Cipher 62 */
505             {
506             1,
507             TLS1_TXT_RSA_EXPORT1024_WITH_DES_CBC_SHA,
508             TLS1_CK_RSA_EXPORT1024_WITH_DES_CBC_SHA,
509             SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA|SSL_TLSV1,
510             SSL_EXPORT|SSL_EXP56,
511             0,
512             56,
513             56,
514             SSL_ALL_CIPHERS,
515             SSL_ALL_STRENGTHS,
516             },
517         /* Cipher 63 */
518             {
519             1,
520             TLS1_TXT_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA,
521             TLS1_CK_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA,
522             SSL_kEDH|SSL_aDSS|SSL_DES|SSL_SHA|SSL_TLSV1,
523             SSL_EXPORT|SSL_EXP56,
524             0,
525             56,
526             56,
527             SSL_ALL_CIPHERS,
528             SSL_ALL_STRENGTHS,
529             },
530         /* Cipher 64 */
531             {
532             1,
533             TLS1_TXT_RSA_EXPORT1024_WITH_RC4_56_SHA,
534             TLS1_CK_RSA_EXPORT1024_WITH_RC4_56_SHA,
535             SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_SHA|SSL_TLSV1,
536             SSL_EXPORT|SSL_EXP56,
537             0,
538             56,
539             128,
540             SSL_ALL_CIPHERS,
541             SSL_ALL_STRENGTHS,
542             },
543         /* Cipher 65 */
544             {
545             1,
546             TLS1_TXT_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA,
547             TLS1_CK_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA,
548             SSL_kEDH|SSL_aDSS|SSL_RC4|SSL_SHA|SSL_TLSV1,
549             SSL_EXPORT|SSL_EXP56,
550             0,
551             56,
552             128,
553             SSL_ALL_CIPHERS,
554             SSL_ALL_STRENGTHS,
555             },
556         /* Cipher 66 */
557             {
558             1,
559             TLS1_TXT_DHE_DSS_WITH_RC4_128_SHA,
560             TLS1_CK_DHE_DSS_WITH_RC4_128_SHA,
561             SSL_kEDH|SSL_aDSS|SSL_RC4|SSL_SHA|SSL_TLSV1,
562             SSL_NOT_EXP,
563             0,
564             128,
565             128,
566             SSL_ALL_CIPHERS,
567             SSL_ALL_STRENGTHS
568             },
569 #endif
570
571 /* end of list */
572         };
573
574 static SSL3_ENC_METHOD SSLv3_enc_data={
575         ssl3_enc,
576         ssl3_mac,
577         ssl3_setup_key_block,
578         ssl3_generate_master_secret,
579         ssl3_change_cipher_state,
580         ssl3_final_finish_mac,
581         MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
582         ssl3_cert_verify_mac,
583         SSL3_MD_CLIENT_FINISHED_CONST,4,
584         SSL3_MD_SERVER_FINISHED_CONST,4,
585         ssl3_alert_code,
586         };
587
588 static SSL_METHOD SSLv3_data= {
589         SSL3_VERSION,
590         ssl3_new,
591         ssl3_clear,
592         ssl3_free,
593         ssl_undefined_function,
594         ssl_undefined_function,
595         ssl3_read,
596         ssl3_peek,
597         ssl3_write,
598         ssl3_shutdown,
599         ssl3_renegotiate,
600         ssl3_renegotiate_check,
601         ssl3_ctrl,
602         ssl3_ctx_ctrl,
603         ssl3_get_cipher_by_char,
604         ssl3_put_cipher_by_char,
605         ssl3_pending,
606         ssl3_num_ciphers,
607         ssl3_get_cipher,
608         ssl_bad_method,
609         ssl3_default_timeout,
610         &SSLv3_enc_data,
611         };
612
613 union rsa_fn_to_char_u
614         {
615         char *char_p;
616         RSA *(*fn_p)(SSL *, int, int);
617         };
618
619 union dh_fn_to_char_u
620         {
621         char *char_p;
622         DH *(*fn_p)(SSL *, int, int);
623         };
624
625 static long ssl3_default_timeout(void)
626         {
627         /* 2 hours, the 24 hours mentioned in the SSLv3 spec
628          * is way too long for http, the cache would over fill */
629         return(60*60*2);
630         }
631
632 SSL_METHOD *sslv3_base_method(void)
633         {
634         return(&SSLv3_data);
635         }
636
637 int ssl3_num_ciphers(void)
638         {
639         return(SSL3_NUM_CIPHERS);
640         }
641
642 SSL_CIPHER *ssl3_get_cipher(unsigned int u)
643         {
644         if (u < SSL3_NUM_CIPHERS)
645                 return(&(ssl3_ciphers[SSL3_NUM_CIPHERS-1-u]));
646         else
647                 return(NULL);
648         }
649
650 /* The problem is that it may not be the correct record type */
651 int ssl3_pending(SSL *s)
652         {
653         return(s->s3->rrec.length);
654         }
655
656 int ssl3_new(SSL *s)
657         {
658         SSL3_STATE *s3;
659
660         if ((s3=Malloc(sizeof *s3)) == NULL) goto err;
661         memset(s3,0,sizeof *s3);
662
663         s->s3=s3;
664
665         s->method->ssl_clear(s);
666         return(1);
667 err:
668         return(0);
669         }
670
671 void ssl3_free(SSL *s)
672         {
673         if(s == NULL)
674             return;
675
676         ssl3_cleanup_key_block(s);
677         if (s->s3->rbuf.buf != NULL)
678                 Free(s->s3->rbuf.buf);
679         if (s->s3->wbuf.buf != NULL)
680                 Free(s->s3->wbuf.buf);
681         if (s->s3->rrec.comp != NULL)
682                 Free(s->s3->rrec.comp);
683 #ifndef NO_DH
684         if (s->s3->tmp.dh != NULL)
685                 DH_free(s->s3->tmp.dh);
686 #endif
687         if (s->s3->tmp.ca_names != NULL)
688                 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
689         memset(s->s3,0,sizeof *s->s3);
690         Free(s->s3);
691         s->s3=NULL;
692         }
693
694 void ssl3_clear(SSL *s)
695         {
696         unsigned char *rp,*wp;
697
698         ssl3_cleanup_key_block(s);
699         if (s->s3->tmp.ca_names != NULL)
700                 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
701
702         if (s->s3->rrec.comp != NULL)
703                 {
704                 Free(s->s3->rrec.comp);
705                 s->s3->rrec.comp=NULL;
706                 }
707
708         rp=s->s3->rbuf.buf;
709         wp=s->s3->wbuf.buf;
710
711         memset(s->s3,0,sizeof *s->s3);
712         if (rp != NULL) s->s3->rbuf.buf=rp;
713         if (wp != NULL) s->s3->wbuf.buf=wp;
714
715         ssl_free_wbio_buffer(s);
716
717         s->packet_length=0;
718         s->s3->renegotiate=0;
719         s->s3->total_renegotiations=0;
720         s->s3->num_renegotiations=0;
721         s->s3->in_read_app_data=0;
722         s->version=SSL3_VERSION;
723         }
724
725 long ssl3_ctrl(SSL *s, int cmd, long larg, char *parg)
726         {
727         int ret=0;
728
729 #if !defined(NO_DSA) || !defined(NO_RSA)
730         if (
731 #ifndef NO_RSA
732             cmd == SSL_CTRL_SET_TMP_RSA ||
733             cmd == SSL_CTRL_SET_TMP_RSA_CB ||
734 #endif
735 #ifndef NO_DSA
736             cmd == SSL_CTRL_SET_TMP_DH ||
737             cmd == SSL_CTRL_SET_TMP_DH_CB ||
738 #endif
739                 0)
740                 {
741                 if (!ssl_cert_inst(&s->cert))
742                         {
743                         SSLerr(SSL_F_SSL3_CTRL, ERR_R_MALLOC_FAILURE);
744                         return(0);
745                         }
746                 }
747 #endif
748
749         switch (cmd)
750                 {
751         case SSL_CTRL_GET_SESSION_REUSED:
752                 ret=s->hit;
753                 break;
754         case SSL_CTRL_GET_CLIENT_CERT_REQUEST:
755                 break;
756         case SSL_CTRL_GET_NUM_RENEGOTIATIONS:
757                 ret=s->s3->num_renegotiations;
758                 break;
759         case SSL_CTRL_CLEAR_NUM_RENEGOTIATIONS:
760                 ret=s->s3->num_renegotiations;
761                 s->s3->num_renegotiations=0;
762                 break;
763         case SSL_CTRL_GET_TOTAL_RENEGOTIATIONS:
764                 ret=s->s3->total_renegotiations;
765                 break;
766         case SSL_CTRL_GET_FLAGS:
767                 ret=(int)(s->s3->flags);
768                 break;
769 #ifndef NO_RSA
770         case SSL_CTRL_NEED_TMP_RSA:
771                 if ((s->cert != NULL) && (s->cert->rsa_tmp == NULL) &&
772                     ((s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) ||
773                      (EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8))))
774                         ret = 1;
775                 break;
776         case SSL_CTRL_SET_TMP_RSA:
777                 {
778                         RSA *rsa = (RSA *)parg;
779                         if (rsa == NULL) {
780                                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
781                                 return(ret);
782                         }
783                         if ((rsa = RSAPrivateKey_dup(rsa)) == NULL) {
784                                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_RSA_LIB);
785                                 return(ret);
786                         }
787                         if (s->cert->rsa_tmp != NULL)
788                                 RSA_free(s->cert->rsa_tmp);
789                         s->cert->rsa_tmp = rsa;
790                         ret = 1;
791                 }
792                 break;
793         case SSL_CTRL_SET_TMP_RSA_CB:
794                 {
795                 union rsa_fn_to_char_u rsa_tmp_cb;
796
797                 rsa_tmp_cb.char_p = parg;
798                 s->cert->rsa_tmp_cb = rsa_tmp_cb.fn_p;
799                 }
800                 break;
801 #endif
802 #ifndef NO_DH
803         case SSL_CTRL_SET_TMP_DH:
804                 {
805                         DH *dh = (DH *)parg;
806                         if (dh == NULL) {
807                                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
808                                 return(ret);
809                         }
810                         if ((dh = DHparams_dup(dh)) == NULL) {
811                                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_DH_LIB);
812                                 return(ret);
813                         }
814                         if (!DH_generate_key(dh)) {
815                                 DH_free(dh);
816                                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_DH_LIB);
817                                 return(ret);
818                         }
819                         if (s->cert->dh_tmp != NULL)
820                                 DH_free(s->cert->dh_tmp);
821                         s->cert->dh_tmp = dh;
822                         ret = 1;
823                 }
824                 break;
825         case SSL_CTRL_SET_TMP_DH_CB:
826                 {
827                 union dh_fn_to_char_u dh_tmp_cb;
828
829                 dh_tmp_cb.char_p = parg;
830                 s->cert->dh_tmp_cb = dh_tmp_cb.fn_p;
831                 }
832                 break;
833 #endif
834         default:
835                 break;
836                 }
837         return(ret);
838         }
839
840 long ssl3_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, char *parg)
841         {
842         CERT *cert;
843
844         cert=ctx->cert;
845
846         switch (cmd)
847                 {
848 #ifndef NO_RSA
849         case SSL_CTRL_NEED_TMP_RSA:
850                 if (    (cert->rsa_tmp == NULL) &&
851                         ((cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) ||
852                          (EVP_PKEY_size(cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8)))
853                         )
854                         return(1);
855                 else
856                         return(0);
857                 /* break; */
858         case SSL_CTRL_SET_TMP_RSA:
859                 {
860                 RSA *rsa;
861                 int i;
862
863                 rsa=(RSA *)parg;
864                 i=1;
865                 if (rsa == NULL)
866                         i=0;
867                 else
868                         {
869                         if ((rsa=RSAPrivateKey_dup(rsa)) == NULL)
870                                 i=0;
871                         }
872                 if (!i)
873                         {
874                         SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_RSA_LIB);
875                         return(0);
876                         }
877                 else
878                         {
879                         if (cert->rsa_tmp != NULL)
880                                 RSA_free(cert->rsa_tmp);
881                         cert->rsa_tmp=rsa;
882                         return(1);
883                         }
884                 }
885                 /* break; */
886         case SSL_CTRL_SET_TMP_RSA_CB:
887                 {
888                 union rsa_fn_to_char_u rsa_tmp_cb;
889
890                 rsa_tmp_cb.char_p = parg;
891                 cert->rsa_tmp_cb = rsa_tmp_cb.fn_p;
892                 }
893                 break;
894 #endif
895 #ifndef NO_DH
896         case SSL_CTRL_SET_TMP_DH:
897                 {
898                 DH *new=NULL,*dh;
899                 int rret=0;
900
901                 dh=(DH *)parg;
902                 if (    ((new=DHparams_dup(dh)) == NULL) ||
903                         (!DH_generate_key(new)))
904                         {
905                         SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_DH_LIB);
906                         if (new != NULL) DH_free(new);
907                         }
908                 else
909                         {
910                         if (cert->dh_tmp != NULL)
911                                 DH_free(cert->dh_tmp);
912                         cert->dh_tmp=new;
913                         rret=1;
914                         }
915                 return(rret);
916                 }
917                 /*break; */
918         case SSL_CTRL_SET_TMP_DH_CB:
919                 {
920                 union dh_fn_to_char_u dh_tmp_cb;
921
922                 dh_tmp_cb.char_p = parg;
923                 cert->dh_tmp_cb = dh_tmp_cb.fn_p;
924                 }
925                 break;
926 #endif
927         /* A Thawte special :-) */
928         case SSL_CTRL_EXTRA_CHAIN_CERT:
929                 if (ctx->extra_certs == NULL)
930                         {
931                         if ((ctx->extra_certs=sk_X509_new_null()) == NULL)
932                                 return(0);
933                         }
934                 sk_X509_push(ctx->extra_certs,(X509 *)parg);
935                 break;
936
937         default:
938                 return(0);
939                 }
940         return(1);
941         }
942
943 /* This function needs to check if the ciphers required are actually
944  * available */
945 SSL_CIPHER *ssl3_get_cipher_by_char(const unsigned char *p)
946         {
947         static int init=1;
948         static SSL_CIPHER *sorted[SSL3_NUM_CIPHERS];
949         SSL_CIPHER c,*cp= &c,**cpp;
950         unsigned long id;
951         int i;
952
953         if (init)
954                 {
955                 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
956
957                 for (i=0; i<SSL3_NUM_CIPHERS; i++)
958                         sorted[i]= &(ssl3_ciphers[i]);
959
960                 qsort(  (char *)sorted,
961                         SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
962                         FP_ICC ssl_cipher_ptr_id_cmp);
963
964                 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
965
966                 init=0;
967                 }
968
969         id=0x03000000L|((unsigned long)p[0]<<8L)|(unsigned long)p[1];
970         c.id=id;
971         cpp=(SSL_CIPHER **)OBJ_bsearch((char *)&cp,
972                 (char *)sorted,
973                 SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
974                 (int (*)())ssl_cipher_ptr_id_cmp);
975         if ((cpp == NULL) || !(*cpp)->valid)
976                 return(NULL);
977         else
978                 return(*cpp);
979         }
980
981 int ssl3_put_cipher_by_char(const SSL_CIPHER *c, unsigned char *p)
982         {
983         long l;
984
985         if (p != NULL)
986                 {
987                 l=c->id;
988                 if ((l & 0xff000000) != 0x03000000) return(0);
989                 p[0]=((unsigned char)(l>> 8L))&0xFF;
990                 p[1]=((unsigned char)(l     ))&0xFF;
991                 }
992         return(2);
993         }
994
995 SSL_CIPHER *ssl3_choose_cipher(SSL *s, STACK_OF(SSL_CIPHER) *have,
996              STACK_OF(SSL_CIPHER) *pref)
997         {
998         SSL_CIPHER *c,*ret=NULL;
999         int i,j,ok;
1000         CERT *cert;
1001         unsigned long alg,mask,emask;
1002
1003         /* Let's see which ciphers we can support */
1004         cert=s->cert;
1005
1006         sk_SSL_CIPHER_set_cmp_func(pref,ssl_cipher_ptr_id_cmp);
1007
1008 #ifdef CIPHER_DEBUG
1009         printf("Have:\n");
1010         for(i=0 ; i < sk_num(pref) ; ++i)
1011             {
1012             c=(SSL_CIPHER *)sk_value(pref,i);
1013             printf("%p:%s\n",c,c->name);
1014             }
1015 #endif
1016
1017         for (i=0; i<sk_SSL_CIPHER_num(have); i++)
1018                 {
1019                 c=sk_SSL_CIPHER_value(have,i);
1020
1021                 ssl_set_cert_masks(cert,c);
1022                 mask=cert->mask;
1023                 emask=cert->export_mask;
1024                         
1025                 alg=c->algorithms&(SSL_MKEY_MASK|SSL_AUTH_MASK);
1026                 if (SSL_C_IS_EXPORT(c))
1027                         {
1028                         ok=((alg & emask) == alg)?1:0;
1029 #ifdef CIPHER_DEBUG
1030                         printf("%d:[%08lX:%08lX]%p:%s (export)\n",ok,alg,emask,
1031                                c,c->name);
1032 #endif
1033                         }
1034                 else
1035                         {
1036                         ok=((alg & mask) == alg)?1:0;
1037 #ifdef CIPHER_DEBUG
1038                         printf("%d:[%08lX:%08lX]%p:%s\n",ok,alg,mask,c,
1039                                c->name);
1040 #endif
1041                         }
1042
1043                 if (!ok) continue;
1044         
1045                 j=sk_SSL_CIPHER_find(pref,c);
1046                 if (j >= 0)
1047                         {
1048                         ret=sk_SSL_CIPHER_value(pref,j);
1049                         break;
1050                         }
1051                 }
1052         return(ret);
1053         }
1054
1055 int ssl3_get_req_cert_type(SSL *s, unsigned char *p)
1056         {
1057         int ret=0;
1058         unsigned long alg;
1059
1060         alg=s->s3->tmp.new_cipher->algorithms;
1061
1062 #ifndef NO_DH
1063         if (alg & (SSL_kDHr|SSL_kEDH))
1064                 {
1065 #  ifndef NO_RSA
1066                 p[ret++]=SSL3_CT_RSA_FIXED_DH;
1067 #  endif
1068 #  ifndef NO_DSA
1069                 p[ret++]=SSL3_CT_DSS_FIXED_DH;
1070 #  endif
1071                 }
1072         if ((s->version == SSL3_VERSION) &&
1073                 (alg & (SSL_kEDH|SSL_kDHd|SSL_kDHr)))
1074                 {
1075 #  ifndef NO_RSA
1076                 p[ret++]=SSL3_CT_RSA_EPHEMERAL_DH;
1077 #  endif
1078 #  ifndef NO_DSA
1079                 p[ret++]=SSL3_CT_DSS_EPHEMERAL_DH;
1080 #  endif
1081                 }
1082 #endif /* !NO_DH */
1083 #ifndef NO_RSA
1084         p[ret++]=SSL3_CT_RSA_SIGN;
1085 #endif
1086 #ifndef NO_DSA
1087         p[ret++]=SSL3_CT_DSS_SIGN;
1088 #endif
1089         return(ret);
1090         }
1091
1092 int ssl3_shutdown(SSL *s)
1093         {
1094
1095         /* Don't do anything much if we have not done the handshake or
1096          * we don't want to send messages :-) */
1097         if ((s->quiet_shutdown) || (s->state == SSL_ST_BEFORE))
1098                 {
1099                 s->shutdown=(SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
1100                 return(1);
1101                 }
1102
1103         if (!(s->shutdown & SSL_SENT_SHUTDOWN))
1104                 {
1105                 s->shutdown|=SSL_SENT_SHUTDOWN;
1106 #if 1
1107                 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_CLOSE_NOTIFY);
1108 #endif
1109                 /* our shutdown alert has been sent now, and if it still needs
1110                  * to be written, s->s3->alert_dispatch will be true */
1111                 }
1112         else if (s->s3->alert_dispatch)
1113                 {
1114                 /* resend it if not sent */
1115 #if 1
1116                 ssl3_dispatch_alert(s);
1117 #endif
1118                 }
1119         else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN))
1120                 {
1121                 /* If we are waiting for a close from our peer, we are closed */
1122                 ssl3_read_bytes(s,0,NULL,0);
1123                 }
1124
1125         if ((s->shutdown == (SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN)) &&
1126                 !s->s3->alert_dispatch)
1127                 return(1);
1128         else
1129                 return(0);
1130         }
1131
1132 int ssl3_write(SSL *s, const void *buf, int len)
1133         {
1134         int ret,n;
1135
1136 #if 0
1137         if (s->shutdown & SSL_SEND_SHUTDOWN)
1138                 {
1139                 s->rwstate=SSL_NOTHING;
1140                 return(0);
1141                 }
1142 #endif
1143         clear_sys_error();
1144         if (s->s3->renegotiate) ssl3_renegotiate_check(s);
1145
1146         /* This is an experimental flag that sends the
1147          * last handshake message in the same packet as the first
1148          * use data - used to see if it helps the TCP protocol during
1149          * session-id reuse */
1150         /* The second test is because the buffer may have been removed */
1151         if ((s->s3->flags & SSL3_FLAGS_POP_BUFFER) && (s->wbio == s->bbio))
1152                 {
1153                 /* First time through, we write into the buffer */
1154                 if (s->s3->delay_buf_pop_ret == 0)
1155                         {
1156                         ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA,
1157                                              buf,len);
1158                         if (ret <= 0) return(ret);
1159
1160                         s->s3->delay_buf_pop_ret=ret;
1161                         }
1162
1163                 s->rwstate=SSL_WRITING;
1164                 n=BIO_flush(s->wbio);
1165                 if (n <= 0) return(n);
1166                 s->rwstate=SSL_NOTHING;
1167
1168                 /* We have flushed the buffer, so remove it */
1169                 ssl_free_wbio_buffer(s);
1170                 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
1171
1172                 ret=s->s3->delay_buf_pop_ret;
1173                 s->s3->delay_buf_pop_ret=0;
1174                 }
1175         else
1176                 {
1177                 ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA,
1178                                      buf,len);
1179                 if (ret <= 0) return(ret);
1180                 }
1181
1182         return(ret);
1183         }
1184
1185 int ssl3_read(SSL *s, void *buf, int len)
1186         {
1187         int ret;
1188         
1189         clear_sys_error();
1190         if (s->s3->renegotiate) ssl3_renegotiate_check(s);
1191         s->s3->in_read_app_data=1;
1192         ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len);
1193         if ((ret == -1) && (s->s3->in_read_app_data == 0))
1194                 {
1195                 /* ssl3_read_bytes decided to call s->handshake_func, which
1196                  * called ssl3_read_bytes to read handshake data.
1197                  * However, ssl3_read_bytes actually found application data
1198                  * and thinks that application data makes sense here (signalled
1199                  * by resetting 'in_read_app_data', strangely); so disable
1200                  * handshake processing and try to read application data again. */
1201                 s->in_handshake++;
1202                 ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len);
1203                 s->in_handshake--;
1204                 }
1205         else
1206                 s->s3->in_read_app_data=0;
1207
1208         return(ret);
1209         }
1210
1211 int ssl3_peek(SSL *s, char *buf, int len)
1212         {
1213         SSL3_RECORD *rr;
1214         int n;
1215
1216         rr= &(s->s3->rrec);
1217         if ((rr->length == 0) || (rr->type != SSL3_RT_APPLICATION_DATA))
1218                 {
1219                 n=ssl3_read(s,buf,1);
1220                 if (n <= 0) return(n);
1221                 rr->length++;
1222                 rr->off--;
1223                 }
1224
1225         if ((unsigned int)len > rr->length)
1226                 n=rr->length;
1227         else
1228                 n=len;
1229         memcpy(buf,&(rr->data[rr->off]),(unsigned int)n);
1230         return(n);
1231         }
1232
1233 int ssl3_renegotiate(SSL *s)
1234         {
1235         if (s->handshake_func == NULL)
1236                 return(1);
1237
1238         if (s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)
1239                 return(0);
1240
1241         s->s3->renegotiate=1;
1242         return(1);
1243         }
1244
1245 int ssl3_renegotiate_check(SSL *s)
1246         {
1247         int ret=0;
1248
1249         if (s->s3->renegotiate)
1250                 {
1251                 if (    (s->s3->rbuf.left == 0) &&
1252                         (s->s3->wbuf.left == 0) &&
1253                         !SSL_in_init(s))
1254                         {
1255 /*
1256 if we are the server, and we have sent a 'RENEGOTIATE' message, we
1257 need to go to SSL_ST_ACCEPT.
1258 */
1259                         /* SSL_ST_ACCEPT */
1260                         s->state=SSL_ST_RENEGOTIATE;
1261                         s->s3->renegotiate=0;
1262                         s->s3->num_renegotiations++;
1263                         s->s3->total_renegotiations++;
1264                         ret=1;
1265                         }
1266                 }
1267         return(ret);
1268         }
1269