Fix SSL memory leak.
[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  * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  *
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer. 
67  *
68  * 2. Redistributions in binary form must reproduce the above copyright
69  *    notice, this list of conditions and the following disclaimer in
70  *    the documentation and/or other materials provided with the
71  *    distribution.
72  *
73  * 3. All advertising materials mentioning features or use of this
74  *    software must display the following acknowledgment:
75  *    "This product includes software developed by the OpenSSL Project
76  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77  *
78  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79  *    endorse or promote products derived from this software without
80  *    prior written permission. For written permission, please contact
81  *    openssl-core@openssl.org.
82  *
83  * 5. Products derived from this software may not be called "OpenSSL"
84  *    nor may "OpenSSL" appear in their names without prior written
85  *    permission of the OpenSSL Project.
86  *
87  * 6. Redistributions of any form whatsoever must retain the following
88  *    acknowledgment:
89  *    "This product includes software developed by the OpenSSL Project
90  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91  *
92  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103  * OF THE POSSIBILITY OF SUCH DAMAGE.
104  * ====================================================================
105  *
106  * This product includes cryptographic software written by Eric Young
107  * (eay@cryptsoft.com).  This product includes software written by Tim
108  * Hudson (tjh@cryptsoft.com).
109  *
110  */
111
112 #include <stdio.h>
113 #include <openssl/objects.h>
114 #include "ssl_locl.h"
115 #include "kssl_lcl.h"
116 #include <openssl/md5.h>
117
118 const char *ssl3_version_str="SSLv3" OPENSSL_VERSION_PTEXT;
119
120 #define SSL3_NUM_CIPHERS        (sizeof(ssl3_ciphers)/sizeof(SSL_CIPHER))
121
122 static long ssl3_default_timeout(void );
123
124 OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
125 /* The RSA ciphers */
126 /* Cipher 01 */
127         {
128         1,
129         SSL3_TXT_RSA_NULL_MD5,
130         SSL3_CK_RSA_NULL_MD5,
131         SSL_kRSA|SSL_aRSA|SSL_eNULL |SSL_MD5|SSL_SSLV3,
132         SSL_NOT_EXP,
133         0,
134         0,
135         0,
136         SSL_ALL_CIPHERS,
137         SSL_ALL_STRENGTHS,
138         },
139 /* Cipher 02 */
140         {
141         1,
142         SSL3_TXT_RSA_NULL_SHA,
143         SSL3_CK_RSA_NULL_SHA,
144         SSL_kRSA|SSL_aRSA|SSL_eNULL |SSL_SHA1|SSL_SSLV3,
145         SSL_NOT_EXP,
146         0,
147         0,
148         0,
149         SSL_ALL_CIPHERS,
150         SSL_ALL_STRENGTHS,
151         },
152
153 /* anon DH */
154 /* Cipher 17 */
155         {
156         1,
157         SSL3_TXT_ADH_RC4_40_MD5,
158         SSL3_CK_ADH_RC4_40_MD5,
159         SSL_kEDH |SSL_aNULL|SSL_RC4  |SSL_MD5 |SSL_SSLV3,
160         SSL_EXPORT|SSL_EXP40,
161         0,
162         40,
163         128,
164         SSL_ALL_CIPHERS,
165         SSL_ALL_STRENGTHS,
166         },
167 /* Cipher 18 */
168         {
169         1,
170         SSL3_TXT_ADH_RC4_128_MD5,
171         SSL3_CK_ADH_RC4_128_MD5,
172         SSL_kEDH |SSL_aNULL|SSL_RC4  |SSL_MD5 |SSL_SSLV3,
173         SSL_NOT_EXP,
174         0,
175         128,
176         128,
177         SSL_ALL_CIPHERS,
178         SSL_ALL_STRENGTHS,
179         },
180 /* Cipher 19 */
181         {
182         1,
183         SSL3_TXT_ADH_DES_40_CBC_SHA,
184         SSL3_CK_ADH_DES_40_CBC_SHA,
185         SSL_kEDH |SSL_aNULL|SSL_DES|SSL_SHA1|SSL_SSLV3,
186         SSL_EXPORT|SSL_EXP40,
187         0,
188         40,
189         128,
190         SSL_ALL_CIPHERS,
191         SSL_ALL_STRENGTHS,
192         },
193 /* Cipher 1A */
194         {
195         1,
196         SSL3_TXT_ADH_DES_64_CBC_SHA,
197         SSL3_CK_ADH_DES_64_CBC_SHA,
198         SSL_kEDH |SSL_aNULL|SSL_DES  |SSL_SHA1|SSL_SSLV3,
199         SSL_NOT_EXP,
200         0,
201         56,
202         56,
203         SSL_ALL_CIPHERS,
204         SSL_ALL_STRENGTHS,
205         },
206 /* Cipher 1B */
207         {
208         1,
209         SSL3_TXT_ADH_DES_192_CBC_SHA,
210         SSL3_CK_ADH_DES_192_CBC_SHA,
211         SSL_kEDH |SSL_aNULL|SSL_3DES |SSL_SHA1|SSL_SSLV3,
212         SSL_NOT_EXP,
213         0,
214         168,
215         168,
216         SSL_ALL_CIPHERS,
217         SSL_ALL_STRENGTHS,
218         },
219
220 /* RSA again */
221 /* Cipher 03 */
222         {
223         1,
224         SSL3_TXT_RSA_RC4_40_MD5,
225         SSL3_CK_RSA_RC4_40_MD5,
226         SSL_kRSA|SSL_aRSA|SSL_RC4  |SSL_MD5 |SSL_SSLV3,
227         SSL_EXPORT|SSL_EXP40,
228         0,
229         40,
230         128,
231         SSL_ALL_CIPHERS,
232         SSL_ALL_STRENGTHS,
233         },
234 /* Cipher 04 */
235         {
236         1,
237         SSL3_TXT_RSA_RC4_128_MD5,
238         SSL3_CK_RSA_RC4_128_MD5,
239         SSL_kRSA|SSL_aRSA|SSL_RC4  |SSL_MD5|SSL_SSLV3,
240         SSL_NOT_EXP|SSL_MEDIUM,
241         0,
242         128,
243         128,
244         SSL_ALL_CIPHERS,
245         SSL_ALL_STRENGTHS,
246         },
247 /* Cipher 05 */
248         {
249         1,
250         SSL3_TXT_RSA_RC4_128_SHA,
251         SSL3_CK_RSA_RC4_128_SHA,
252         SSL_kRSA|SSL_aRSA|SSL_RC4  |SSL_SHA1|SSL_SSLV3,
253         SSL_NOT_EXP|SSL_MEDIUM,
254         0,
255         128,
256         128,
257         SSL_ALL_CIPHERS,
258         SSL_ALL_STRENGTHS,
259         },
260 /* Cipher 06 */
261         {
262         1,
263         SSL3_TXT_RSA_RC2_40_MD5,
264         SSL3_CK_RSA_RC2_40_MD5,
265         SSL_kRSA|SSL_aRSA|SSL_RC2  |SSL_MD5 |SSL_SSLV3,
266         SSL_EXPORT|SSL_EXP40,
267         0,
268         40,
269         128,
270         SSL_ALL_CIPHERS,
271         SSL_ALL_STRENGTHS,
272         },
273 /* Cipher 07 */
274         {
275         1,
276         SSL3_TXT_RSA_IDEA_128_SHA,
277         SSL3_CK_RSA_IDEA_128_SHA,
278         SSL_kRSA|SSL_aRSA|SSL_IDEA |SSL_SHA1|SSL_SSLV3,
279         SSL_NOT_EXP|SSL_MEDIUM,
280         0,
281         128,
282         128,
283         SSL_ALL_CIPHERS,
284         SSL_ALL_STRENGTHS,
285         },
286 /* Cipher 08 */
287         {
288         1,
289         SSL3_TXT_RSA_DES_40_CBC_SHA,
290         SSL3_CK_RSA_DES_40_CBC_SHA,
291         SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA1|SSL_SSLV3,
292         SSL_EXPORT|SSL_EXP40,
293         0,
294         40,
295         56,
296         SSL_ALL_CIPHERS,
297         SSL_ALL_STRENGTHS,
298         },
299 /* Cipher 09 */
300         {
301         1,
302         SSL3_TXT_RSA_DES_64_CBC_SHA,
303         SSL3_CK_RSA_DES_64_CBC_SHA,
304         SSL_kRSA|SSL_aRSA|SSL_DES  |SSL_SHA1|SSL_SSLV3,
305         SSL_NOT_EXP|SSL_LOW,
306         0,
307         56,
308         56,
309         SSL_ALL_CIPHERS,
310         SSL_ALL_STRENGTHS,
311         },
312 /* Cipher 0A */
313         {
314         1,
315         SSL3_TXT_RSA_DES_192_CBC3_SHA,
316         SSL3_CK_RSA_DES_192_CBC3_SHA,
317         SSL_kRSA|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_SSLV3,
318         SSL_NOT_EXP|SSL_HIGH,
319         0,
320         168,
321         168,
322         SSL_ALL_CIPHERS,
323         SSL_ALL_STRENGTHS,
324         },
325
326 /*  The DH ciphers */
327 /* Cipher 0B */
328         {
329         0,
330         SSL3_TXT_DH_DSS_DES_40_CBC_SHA,
331         SSL3_CK_DH_DSS_DES_40_CBC_SHA,
332         SSL_kDHd |SSL_aDH|SSL_DES|SSL_SHA1|SSL_SSLV3,
333         SSL_EXPORT|SSL_EXP40,
334         0,
335         40,
336         56,
337         SSL_ALL_CIPHERS,
338         SSL_ALL_STRENGTHS,
339         },
340 /* Cipher 0C */
341         {
342         0,
343         SSL3_TXT_DH_DSS_DES_64_CBC_SHA,
344         SSL3_CK_DH_DSS_DES_64_CBC_SHA,
345         SSL_kDHd |SSL_aDH|SSL_DES  |SSL_SHA1|SSL_SSLV3,
346         SSL_NOT_EXP|SSL_LOW,
347         0,
348         56,
349         56,
350         SSL_ALL_CIPHERS,
351         SSL_ALL_STRENGTHS,
352         },
353 /* Cipher 0D */
354         {
355         0,
356         SSL3_TXT_DH_DSS_DES_192_CBC3_SHA,
357         SSL3_CK_DH_DSS_DES_192_CBC3_SHA,
358         SSL_kDHd |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_SSLV3,
359         SSL_NOT_EXP|SSL_HIGH,
360         0,
361         168,
362         168,
363         SSL_ALL_CIPHERS,
364         SSL_ALL_STRENGTHS,
365         },
366 /* Cipher 0E */
367         {
368         0,
369         SSL3_TXT_DH_RSA_DES_40_CBC_SHA,
370         SSL3_CK_DH_RSA_DES_40_CBC_SHA,
371         SSL_kDHr |SSL_aDH|SSL_DES|SSL_SHA1|SSL_SSLV3,
372         SSL_EXPORT|SSL_EXP40,
373         0,
374         40,
375         56,
376         SSL_ALL_CIPHERS,
377         SSL_ALL_STRENGTHS,
378         },
379 /* Cipher 0F */
380         {
381         0,
382         SSL3_TXT_DH_RSA_DES_64_CBC_SHA,
383         SSL3_CK_DH_RSA_DES_64_CBC_SHA,
384         SSL_kDHr |SSL_aDH|SSL_DES  |SSL_SHA1|SSL_SSLV3,
385         SSL_NOT_EXP|SSL_LOW,
386         0,
387         56,
388         56,
389         SSL_ALL_CIPHERS,
390         SSL_ALL_STRENGTHS,
391         },
392 /* Cipher 10 */
393         {
394         0,
395         SSL3_TXT_DH_RSA_DES_192_CBC3_SHA,
396         SSL3_CK_DH_RSA_DES_192_CBC3_SHA,
397         SSL_kDHr |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_SSLV3,
398         SSL_NOT_EXP|SSL_HIGH,
399         0,
400         168,
401         168,
402         SSL_ALL_CIPHERS,
403         SSL_ALL_STRENGTHS,
404         },
405
406 /* The Ephemeral DH ciphers */
407 /* Cipher 11 */
408         {
409         1,
410         SSL3_TXT_EDH_DSS_DES_40_CBC_SHA,
411         SSL3_CK_EDH_DSS_DES_40_CBC_SHA,
412         SSL_kEDH|SSL_aDSS|SSL_DES|SSL_SHA1|SSL_SSLV3,
413         SSL_EXPORT|SSL_EXP40,
414         0,
415         40,
416         56,
417         SSL_ALL_CIPHERS,
418         SSL_ALL_STRENGTHS,
419         },
420 /* Cipher 12 */
421         {
422         1,
423         SSL3_TXT_EDH_DSS_DES_64_CBC_SHA,
424         SSL3_CK_EDH_DSS_DES_64_CBC_SHA,
425         SSL_kEDH|SSL_aDSS|SSL_DES  |SSL_SHA1|SSL_SSLV3,
426         SSL_NOT_EXP|SSL_LOW,
427         0,
428         56,
429         56,
430         SSL_ALL_CIPHERS,
431         SSL_ALL_STRENGTHS,
432         },
433 /* Cipher 13 */
434         {
435         1,
436         SSL3_TXT_EDH_DSS_DES_192_CBC3_SHA,
437         SSL3_CK_EDH_DSS_DES_192_CBC3_SHA,
438         SSL_kEDH|SSL_aDSS|SSL_3DES |SSL_SHA1|SSL_SSLV3,
439         SSL_NOT_EXP|SSL_HIGH,
440         0,
441         168,
442         168,
443         SSL_ALL_CIPHERS,
444         SSL_ALL_STRENGTHS,
445         },
446 /* Cipher 14 */
447         {
448         1,
449         SSL3_TXT_EDH_RSA_DES_40_CBC_SHA,
450         SSL3_CK_EDH_RSA_DES_40_CBC_SHA,
451         SSL_kEDH|SSL_aRSA|SSL_DES|SSL_SHA1|SSL_SSLV3,
452         SSL_EXPORT|SSL_EXP40,
453         0,
454         40,
455         56,
456         SSL_ALL_CIPHERS,
457         SSL_ALL_STRENGTHS,
458         },
459 /* Cipher 15 */
460         {
461         1,
462         SSL3_TXT_EDH_RSA_DES_64_CBC_SHA,
463         SSL3_CK_EDH_RSA_DES_64_CBC_SHA,
464         SSL_kEDH|SSL_aRSA|SSL_DES  |SSL_SHA1|SSL_SSLV3,
465         SSL_NOT_EXP|SSL_LOW,
466         0,
467         56,
468         56,
469         SSL_ALL_CIPHERS,
470         SSL_ALL_STRENGTHS,
471         },
472 /* Cipher 16 */
473         {
474         1,
475         SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA,
476         SSL3_CK_EDH_RSA_DES_192_CBC3_SHA,
477         SSL_kEDH|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_SSLV3,
478         SSL_NOT_EXP|SSL_HIGH,
479         0,
480         168,
481         168,
482         SSL_ALL_CIPHERS,
483         SSL_ALL_STRENGTHS,
484         },
485
486 /* Fortezza */
487 /* Cipher 1C */
488         {
489         0,
490         SSL3_TXT_FZA_DMS_NULL_SHA,
491         SSL3_CK_FZA_DMS_NULL_SHA,
492         SSL_kFZA|SSL_aFZA |SSL_eNULL |SSL_SHA1|SSL_SSLV3,
493         SSL_NOT_EXP,
494         0,
495         0,
496         0,
497         SSL_ALL_CIPHERS,
498         SSL_ALL_STRENGTHS,
499         },
500
501 /* Cipher 1D */
502         {
503         0,
504         SSL3_TXT_FZA_DMS_FZA_SHA,
505         SSL3_CK_FZA_DMS_FZA_SHA,
506         SSL_kFZA|SSL_aFZA |SSL_eFZA |SSL_SHA1|SSL_SSLV3,
507         SSL_NOT_EXP,
508         0,
509         0,
510         0,
511         SSL_ALL_CIPHERS,
512         SSL_ALL_STRENGTHS,
513         },
514
515 /* Cipher 1E */
516         {
517         0,
518         SSL3_TXT_FZA_DMS_RC4_SHA,
519         SSL3_CK_FZA_DMS_RC4_SHA,
520         SSL_kFZA|SSL_aFZA |SSL_RC4  |SSL_SHA1|SSL_SSLV3,
521         SSL_NOT_EXP,
522         0,
523         128,
524         128,
525         SSL_ALL_CIPHERS,
526         SSL_ALL_STRENGTHS,
527         },
528
529 #ifndef OPENSSL_NO_KRB5
530 /* The Kerberos ciphers
531 ** 20000107 VRS: And the first shall be last,
532 ** in hopes of avoiding the lynx ssl renegotiation problem.
533 */
534 /* Cipher 21 VRS */
535         {
536         1,
537         SSL3_TXT_KRB5_DES_40_CBC_SHA,
538         SSL3_CK_KRB5_DES_40_CBC_SHA,
539         SSL_kKRB5|SSL_aKRB5|  SSL_DES|SSL_SHA1   |SSL_SSLV3,
540         SSL_EXPORT|SSL_EXP40,
541         0,
542         40,
543         56,
544         SSL_ALL_CIPHERS,
545         SSL_ALL_STRENGTHS,
546         },
547
548 /* Cipher 22 VRS */
549         {
550         1,
551         SSL3_TXT_KRB5_DES_40_CBC_MD5,
552         SSL3_CK_KRB5_DES_40_CBC_MD5,
553         SSL_kKRB5|SSL_aKRB5|  SSL_DES|SSL_MD5    |SSL_SSLV3,
554         SSL_EXPORT|SSL_EXP40,
555         0,
556         40,
557         56,
558         SSL_ALL_CIPHERS,
559         SSL_ALL_STRENGTHS,
560         },
561
562 /* Cipher 23 VRS */
563         {
564         1,
565         SSL3_TXT_KRB5_DES_64_CBC_SHA,
566         SSL3_CK_KRB5_DES_64_CBC_SHA,
567         SSL_kKRB5|SSL_aKRB5|  SSL_DES|SSL_SHA1   |SSL_SSLV3,
568         SSL_NOT_EXP|SSL_LOW,
569         0,
570         56,
571         56,
572         SSL_ALL_CIPHERS,
573         SSL_ALL_STRENGTHS,
574         },
575
576 /* Cipher 24 VRS */
577         {
578         1,
579         SSL3_TXT_KRB5_DES_64_CBC_MD5,
580         SSL3_CK_KRB5_DES_64_CBC_MD5,
581         SSL_kKRB5|SSL_aKRB5|  SSL_DES|SSL_MD5    |SSL_SSLV3,
582         SSL_NOT_EXP|SSL_LOW,
583         0,
584         56,
585         56,
586         SSL_ALL_CIPHERS,
587         SSL_ALL_STRENGTHS,
588         },
589
590 /* Cipher 25 VRS */
591         {
592         1,
593         SSL3_TXT_KRB5_DES_192_CBC3_SHA,
594         SSL3_CK_KRB5_DES_192_CBC3_SHA,
595         SSL_kKRB5|SSL_aKRB5|  SSL_3DES|SSL_SHA1  |SSL_SSLV3,
596         SSL_NOT_EXP|SSL_HIGH,
597         0,
598         112,
599         168,
600         SSL_ALL_CIPHERS,
601         SSL_ALL_STRENGTHS,
602         },
603
604 /* Cipher 26 VRS */
605         {
606         1,
607         SSL3_TXT_KRB5_DES_192_CBC3_MD5,
608         SSL3_CK_KRB5_DES_192_CBC3_MD5,
609         SSL_kKRB5|SSL_aKRB5|  SSL_3DES|SSL_MD5   |SSL_SSLV3,
610         SSL_NOT_EXP|SSL_HIGH,
611         0,
612         112,
613         168,
614         SSL_ALL_CIPHERS,
615         SSL_ALL_STRENGTHS,
616         },
617 #endif  /* OPENSSL_NO_KRB5 */
618
619
620 #if TLS1_ALLOW_EXPERIMENTAL_CIPHERSUITES
621         /* New TLS Export CipherSuites */
622         /* Cipher 60 */
623             {
624             1,
625             TLS1_TXT_RSA_EXPORT1024_WITH_RC4_56_MD5,
626             TLS1_CK_RSA_EXPORT1024_WITH_RC4_56_MD5,
627             SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_TLSV1,
628             SSL_EXPORT|SSL_EXP56,
629             0,
630             56,
631             128,
632             SSL_ALL_CIPHERS,
633             SSL_ALL_STRENGTHS,
634             },
635         /* Cipher 61 */
636             {
637             1,
638             TLS1_TXT_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5,
639             TLS1_CK_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5,
640             SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5|SSL_TLSV1,
641             SSL_EXPORT|SSL_EXP56,
642             0,
643             56,
644             128,
645             SSL_ALL_CIPHERS,
646             SSL_ALL_STRENGTHS,
647             },
648         /* Cipher 62 */
649             {
650             1,
651             TLS1_TXT_RSA_EXPORT1024_WITH_DES_CBC_SHA,
652             TLS1_CK_RSA_EXPORT1024_WITH_DES_CBC_SHA,
653             SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA|SSL_TLSV1,
654             SSL_EXPORT|SSL_EXP56,
655             0,
656             56,
657             56,
658             SSL_ALL_CIPHERS,
659             SSL_ALL_STRENGTHS,
660             },
661         /* Cipher 63 */
662             {
663             1,
664             TLS1_TXT_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA,
665             TLS1_CK_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA,
666             SSL_kEDH|SSL_aDSS|SSL_DES|SSL_SHA|SSL_TLSV1,
667             SSL_EXPORT|SSL_EXP56,
668             0,
669             56,
670             56,
671             SSL_ALL_CIPHERS,
672             SSL_ALL_STRENGTHS,
673             },
674         /* Cipher 64 */
675             {
676             1,
677             TLS1_TXT_RSA_EXPORT1024_WITH_RC4_56_SHA,
678             TLS1_CK_RSA_EXPORT1024_WITH_RC4_56_SHA,
679             SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_SHA|SSL_TLSV1,
680             SSL_EXPORT|SSL_EXP56,
681             0,
682             56,
683             128,
684             SSL_ALL_CIPHERS,
685             SSL_ALL_STRENGTHS,
686             },
687         /* Cipher 65 */
688             {
689             1,
690             TLS1_TXT_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA,
691             TLS1_CK_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA,
692             SSL_kEDH|SSL_aDSS|SSL_RC4|SSL_SHA|SSL_TLSV1,
693             SSL_EXPORT|SSL_EXP56,
694             0,
695             56,
696             128,
697             SSL_ALL_CIPHERS,
698             SSL_ALL_STRENGTHS,
699             },
700         /* Cipher 66 */
701             {
702             1,
703             TLS1_TXT_DHE_DSS_WITH_RC4_128_SHA,
704             TLS1_CK_DHE_DSS_WITH_RC4_128_SHA,
705             SSL_kEDH|SSL_aDSS|SSL_RC4|SSL_SHA|SSL_TLSV1,
706             SSL_NOT_EXP,
707             0,
708             128,
709             128,
710             SSL_ALL_CIPHERS,
711             SSL_ALL_STRENGTHS
712             },
713 #endif
714         /* New AES ciphersuites */
715
716         /* Cipher 2F */
717             {
718             1,
719             TLS1_TXT_RSA_WITH_AES_128_SHA,
720             TLS1_CK_RSA_WITH_AES_128_SHA,
721             SSL_kRSA|SSL_aRSA|SSL_AES|SSL_SHA |SSL_TLSV1,
722             SSL_NOT_EXP|SSL_MEDIUM,
723             0,
724             128,
725             128,
726             SSL_ALL_CIPHERS,
727             SSL_ALL_STRENGTHS,
728             },
729         /* Cipher 30 */
730             {
731             0,
732             TLS1_TXT_DH_DSS_WITH_AES_128_SHA,
733             TLS1_CK_DH_DSS_WITH_AES_128_SHA,
734             SSL_kDHd|SSL_aDH|SSL_AES|SSL_SHA|SSL_TLSV1,
735             SSL_NOT_EXP|SSL_MEDIUM,
736             0,
737             128,
738             128,
739             SSL_ALL_CIPHERS,
740             SSL_ALL_STRENGTHS,
741             },
742         /* Cipher 31 */
743             {
744             0,
745             TLS1_TXT_DH_RSA_WITH_AES_128_SHA,
746             TLS1_CK_DH_RSA_WITH_AES_128_SHA,
747             SSL_kDHr|SSL_aDH|SSL_AES|SSL_SHA|SSL_TLSV1,
748             SSL_NOT_EXP|SSL_MEDIUM,
749             0,
750             128,
751             128,
752             SSL_ALL_CIPHERS,
753             SSL_ALL_STRENGTHS,
754             },
755         /* Cipher 32 */
756             {
757             1,
758             TLS1_TXT_DHE_DSS_WITH_AES_128_SHA,
759             TLS1_CK_DHE_DSS_WITH_AES_128_SHA,
760             SSL_kEDH|SSL_aDSS|SSL_AES|SSL_SHA|SSL_TLSV1,
761             SSL_NOT_EXP|SSL_MEDIUM,
762             0,
763             128,
764             128,
765             SSL_ALL_CIPHERS,
766             SSL_ALL_STRENGTHS,
767             },
768         /* Cipher 33 */
769             {
770             1,
771             TLS1_TXT_DHE_RSA_WITH_AES_128_SHA,
772             TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
773             SSL_kEDH|SSL_aRSA|SSL_AES|SSL_SHA|SSL_TLSV1,
774             SSL_NOT_EXP|SSL_MEDIUM,
775             0,
776             128,
777             128,
778             SSL_ALL_CIPHERS,
779             SSL_ALL_STRENGTHS,
780             },
781         /* Cipher 34 */
782             {
783             1,
784             TLS1_TXT_ADH_WITH_AES_128_SHA,
785             TLS1_CK_ADH_WITH_AES_128_SHA,
786             SSL_kEDH|SSL_aNULL|SSL_AES|SSL_SHA|SSL_TLSV1,
787             SSL_NOT_EXP|SSL_MEDIUM,
788             0,
789             128,
790             128,
791             SSL_ALL_CIPHERS,
792             SSL_ALL_STRENGTHS,
793             },
794
795         /* Cipher 35 */
796             {
797             1,
798             TLS1_TXT_RSA_WITH_AES_256_SHA,
799             TLS1_CK_RSA_WITH_AES_256_SHA,
800             SSL_kRSA|SSL_aRSA|SSL_AES|SSL_SHA |SSL_TLSV1,
801             SSL_NOT_EXP|SSL_HIGH,
802             0,
803             256,
804             256,
805             SSL_ALL_CIPHERS,
806             SSL_ALL_STRENGTHS,
807             },
808         /* Cipher 36 */
809             {
810             0,
811             TLS1_TXT_DH_DSS_WITH_AES_256_SHA,
812             TLS1_CK_DH_DSS_WITH_AES_256_SHA,
813             SSL_kDHd|SSL_aDH|SSL_AES|SSL_SHA|SSL_TLSV1,
814             SSL_NOT_EXP|SSL_HIGH,
815             0,
816             256,
817             256,
818             SSL_ALL_CIPHERS,
819             SSL_ALL_STRENGTHS,
820             },
821         /* Cipher 37 */
822             {
823             0,
824             TLS1_TXT_DH_RSA_WITH_AES_256_SHA,
825             TLS1_CK_DH_RSA_WITH_AES_256_SHA,
826             SSL_kDHr|SSL_aDH|SSL_AES|SSL_SHA|SSL_TLSV1,
827             SSL_NOT_EXP|SSL_HIGH,
828             0,
829             256,
830             256,
831             SSL_ALL_CIPHERS,
832             SSL_ALL_STRENGTHS,
833             },
834         /* Cipher 38 */
835             {
836             1,
837             TLS1_TXT_DHE_DSS_WITH_AES_256_SHA,
838             TLS1_CK_DHE_DSS_WITH_AES_256_SHA,
839             SSL_kEDH|SSL_aDSS|SSL_AES|SSL_SHA|SSL_TLSV1,
840             SSL_NOT_EXP|SSL_HIGH,
841             0,
842             256,
843             256,
844             SSL_ALL_CIPHERS,
845             SSL_ALL_STRENGTHS,
846             },
847         /* Cipher 39 */
848             {
849             1,
850             TLS1_TXT_DHE_RSA_WITH_AES_256_SHA,
851             TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
852             SSL_kEDH|SSL_aRSA|SSL_AES|SSL_SHA|SSL_TLSV1,
853             SSL_NOT_EXP|SSL_HIGH,
854             0,
855             256,
856             256,
857             SSL_ALL_CIPHERS,
858             SSL_ALL_STRENGTHS,
859             },
860         /* Cipher 3A */
861             {
862             1,
863             TLS1_TXT_ADH_WITH_AES_256_SHA,
864             TLS1_CK_ADH_WITH_AES_256_SHA,
865             SSL_kEDH|SSL_aNULL|SSL_AES|SSL_SHA|SSL_TLSV1,
866             SSL_NOT_EXP|SSL_HIGH,
867             0,
868             256,
869             256,
870             SSL_ALL_CIPHERS,
871             SSL_ALL_STRENGTHS,
872             },
873
874 /* end of list */
875         };
876
877 static SSL3_ENC_METHOD SSLv3_enc_data={
878         ssl3_enc,
879         ssl3_mac,
880         ssl3_setup_key_block,
881         ssl3_generate_master_secret,
882         ssl3_change_cipher_state,
883         ssl3_final_finish_mac,
884         MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
885         ssl3_cert_verify_mac,
886         SSL3_MD_CLIENT_FINISHED_CONST,4,
887         SSL3_MD_SERVER_FINISHED_CONST,4,
888         ssl3_alert_code,
889         };
890
891 static SSL_METHOD SSLv3_data= {
892         SSL3_VERSION,
893         ssl3_new,
894         ssl3_clear,
895         ssl3_free,
896         ssl_undefined_function,
897         ssl_undefined_function,
898         ssl3_read,
899         ssl3_peek,
900         ssl3_write,
901         ssl3_shutdown,
902         ssl3_renegotiate,
903         ssl3_renegotiate_check,
904         ssl3_ctrl,
905         ssl3_ctx_ctrl,
906         ssl3_get_cipher_by_char,
907         ssl3_put_cipher_by_char,
908         ssl3_pending,
909         ssl3_num_ciphers,
910         ssl3_get_cipher,
911         ssl_bad_method,
912         ssl3_default_timeout,
913         &SSLv3_enc_data,
914         ssl_undefined_function,
915         ssl3_callback_ctrl,
916         ssl3_ctx_callback_ctrl,
917         };
918
919 static long ssl3_default_timeout(void)
920         {
921         /* 2 hours, the 24 hours mentioned in the SSLv3 spec
922          * is way too long for http, the cache would over fill */
923         return(60*60*2);
924         }
925
926 SSL_METHOD *sslv3_base_method(void)
927         {
928         return(&SSLv3_data);
929         }
930
931 int ssl3_num_ciphers(void)
932         {
933         return(SSL3_NUM_CIPHERS);
934         }
935
936 SSL_CIPHER *ssl3_get_cipher(unsigned int u)
937         {
938         if (u < SSL3_NUM_CIPHERS)
939                 return(&(ssl3_ciphers[SSL3_NUM_CIPHERS-1-u]));
940         else
941                 return(NULL);
942         }
943
944 int ssl3_pending(SSL *s)
945         {
946         return (s->s3->rrec.type == SSL3_RT_APPLICATION_DATA) ? s->s3->rrec.length : 0;
947         }
948
949 int ssl3_new(SSL *s)
950         {
951         SSL3_STATE *s3;
952
953         if ((s3=OPENSSL_malloc(sizeof *s3)) == NULL) goto err;
954         memset(s3,0,sizeof *s3);
955         EVP_MD_CTX_init(&s3->finish_dgst1);
956         EVP_MD_CTX_init(&s3->finish_dgst2);
957
958         s->s3=s3;
959
960         s->method->ssl_clear(s);
961         return(1);
962 err:
963         return(0);
964         }
965
966 void ssl3_free(SSL *s)
967         {
968         if(s == NULL)
969             return;
970
971         ssl3_cleanup_key_block(s);
972         if (s->s3->rbuf.buf != NULL)
973                 OPENSSL_free(s->s3->rbuf.buf);
974         if (s->s3->wbuf.buf != NULL)
975                 OPENSSL_free(s->s3->wbuf.buf);
976         if (s->s3->rrec.comp != NULL)
977                 OPENSSL_free(s->s3->rrec.comp);
978 #ifndef OPENSSL_NO_DH
979         if (s->s3->tmp.dh != NULL)
980                 DH_free(s->s3->tmp.dh);
981 #endif
982         if (s->s3->tmp.ca_names != NULL)
983                 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
984         EVP_MD_CTX_cleanup(&s->s3->finish_dgst1);
985         EVP_MD_CTX_cleanup(&s->s3->finish_dgst2);
986         memset(s->s3,0,sizeof *s->s3);
987         OPENSSL_free(s->s3);
988         s->s3=NULL;
989         }
990
991 void ssl3_clear(SSL *s)
992         {
993         unsigned char *rp,*wp;
994
995         ssl3_cleanup_key_block(s);
996         if (s->s3->tmp.ca_names != NULL)
997                 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
998
999         if (s->s3->rrec.comp != NULL)
1000                 {
1001                 OPENSSL_free(s->s3->rrec.comp);
1002                 s->s3->rrec.comp=NULL;
1003                 }
1004 #ifndef OPENSSL_NO_DH
1005         if (s->s3->tmp.dh != NULL)
1006                 DH_free(s->s3->tmp.dh);
1007 #endif
1008
1009         rp=s->s3->rbuf.buf;
1010         wp=s->s3->wbuf.buf;
1011
1012         EVP_MD_CTX_cleanup(&s->s3->finish_dgst1);
1013         EVP_MD_CTX_cleanup(&s->s3->finish_dgst2);
1014
1015         memset(s->s3,0,sizeof *s->s3);
1016         if (rp != NULL) s->s3->rbuf.buf=rp;
1017         if (wp != NULL) s->s3->wbuf.buf=wp;
1018
1019         ssl_free_wbio_buffer(s);
1020
1021         s->packet_length=0;
1022         s->s3->renegotiate=0;
1023         s->s3->total_renegotiations=0;
1024         s->s3->num_renegotiations=0;
1025         s->s3->in_read_app_data=0;
1026         s->version=SSL3_VERSION;
1027         }
1028
1029 long ssl3_ctrl(SSL *s, int cmd, long larg, char *parg)
1030         {
1031         int ret=0;
1032
1033 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_RSA)
1034         if (
1035 #ifndef OPENSSL_NO_RSA
1036             cmd == SSL_CTRL_SET_TMP_RSA ||
1037             cmd == SSL_CTRL_SET_TMP_RSA_CB ||
1038 #endif
1039 #ifndef OPENSSL_NO_DSA
1040             cmd == SSL_CTRL_SET_TMP_DH ||
1041             cmd == SSL_CTRL_SET_TMP_DH_CB ||
1042 #endif
1043                 0)
1044                 {
1045                 if (!ssl_cert_inst(&s->cert))
1046                         {
1047                         SSLerr(SSL_F_SSL3_CTRL, ERR_R_MALLOC_FAILURE);
1048                         return(0);
1049                         }
1050                 }
1051 #endif
1052
1053         switch (cmd)
1054                 {
1055         case SSL_CTRL_GET_SESSION_REUSED:
1056                 ret=s->hit;
1057                 break;
1058         case SSL_CTRL_GET_CLIENT_CERT_REQUEST:
1059                 break;
1060         case SSL_CTRL_GET_NUM_RENEGOTIATIONS:
1061                 ret=s->s3->num_renegotiations;
1062                 break;
1063         case SSL_CTRL_CLEAR_NUM_RENEGOTIATIONS:
1064                 ret=s->s3->num_renegotiations;
1065                 s->s3->num_renegotiations=0;
1066                 break;
1067         case SSL_CTRL_GET_TOTAL_RENEGOTIATIONS:
1068                 ret=s->s3->total_renegotiations;
1069                 break;
1070         case SSL_CTRL_GET_FLAGS:
1071                 ret=(int)(s->s3->flags);
1072                 break;
1073 #ifndef OPENSSL_NO_RSA
1074         case SSL_CTRL_NEED_TMP_RSA:
1075                 if ((s->cert != NULL) && (s->cert->rsa_tmp == NULL) &&
1076                     ((s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) ||
1077                      (EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8))))
1078                         ret = 1;
1079                 break;
1080         case SSL_CTRL_SET_TMP_RSA:
1081                 {
1082                         RSA *rsa = (RSA *)parg;
1083                         if (rsa == NULL)
1084                                 {
1085                                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
1086                                 return(ret);
1087                                 }
1088                         if ((rsa = RSAPrivateKey_dup(rsa)) == NULL)
1089                                 {
1090                                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_RSA_LIB);
1091                                 return(ret);
1092                                 }
1093                         if (s->cert->rsa_tmp != NULL)
1094                                 RSA_free(s->cert->rsa_tmp);
1095                         s->cert->rsa_tmp = rsa;
1096                         ret = 1;
1097                 }
1098                 break;
1099         case SSL_CTRL_SET_TMP_RSA_CB:
1100                 {
1101                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1102                 return(ret);
1103                 }
1104                 break;
1105 #endif
1106 #ifndef OPENSSL_NO_DH
1107         case SSL_CTRL_SET_TMP_DH:
1108                 {
1109                         DH *dh = (DH *)parg;
1110                         if (dh == NULL)
1111                                 {
1112                                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
1113                                 return(ret);
1114                                 }
1115                         if ((dh = DHparams_dup(dh)) == NULL)
1116                                 {
1117                                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_DH_LIB);
1118                                 return(ret);
1119                                 }
1120                         if (!(s->options & SSL_OP_SINGLE_DH_USE))
1121                                 {
1122                                 if (!DH_generate_key(dh))
1123                                         {
1124                                         DH_free(dh);
1125                                         SSLerr(SSL_F_SSL3_CTRL, ERR_R_DH_LIB);
1126                                         return(ret);
1127                                         }
1128                                 }
1129                         if (s->cert->dh_tmp != NULL)
1130                                 DH_free(s->cert->dh_tmp);
1131                         s->cert->dh_tmp = dh;
1132                         ret = 1;
1133                 }
1134                 break;
1135         case SSL_CTRL_SET_TMP_DH_CB:
1136                 {
1137                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1138                 return(ret);
1139                 }
1140                 break;
1141 #endif
1142         default:
1143                 break;
1144                 }
1145         return(ret);
1146         }
1147
1148 long ssl3_callback_ctrl(SSL *s, int cmd, void (*fp)())
1149         {
1150         int ret=0;
1151
1152 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_RSA)
1153         if (
1154 #ifndef OPENSSL_NO_RSA
1155             cmd == SSL_CTRL_SET_TMP_RSA_CB ||
1156 #endif
1157 #ifndef OPENSSL_NO_DSA
1158             cmd == SSL_CTRL_SET_TMP_DH_CB ||
1159 #endif
1160                 0)
1161                 {
1162                 if (!ssl_cert_inst(&s->cert))
1163                         {
1164                         SSLerr(SSL_F_SSL3_CALLBACK_CTRL, ERR_R_MALLOC_FAILURE);
1165                         return(0);
1166                         }
1167                 }
1168 #endif
1169
1170         switch (cmd)
1171                 {
1172 #ifndef OPENSSL_NO_RSA
1173         case SSL_CTRL_SET_TMP_RSA_CB:
1174                 {
1175                 s->cert->rsa_tmp_cb = (RSA *(*)(SSL *, int, int))fp;
1176                 }
1177                 break;
1178 #endif
1179 #ifndef OPENSSL_NO_DH
1180         case SSL_CTRL_SET_TMP_DH_CB:
1181                 {
1182                 s->cert->dh_tmp_cb = (DH *(*)(SSL *, int, int))fp;
1183                 }
1184                 break;
1185 #endif
1186         default:
1187                 break;
1188                 }
1189         return(ret);
1190         }
1191
1192 long ssl3_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, char *parg)
1193         {
1194         CERT *cert;
1195
1196         cert=ctx->cert;
1197
1198         switch (cmd)
1199                 {
1200 #ifndef OPENSSL_NO_RSA
1201         case SSL_CTRL_NEED_TMP_RSA:
1202                 if (    (cert->rsa_tmp == NULL) &&
1203                         ((cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) ||
1204                          (EVP_PKEY_size(cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8)))
1205                         )
1206                         return(1);
1207                 else
1208                         return(0);
1209                 /* break; */
1210         case SSL_CTRL_SET_TMP_RSA:
1211                 {
1212                 RSA *rsa;
1213                 int i;
1214
1215                 rsa=(RSA *)parg;
1216                 i=1;
1217                 if (rsa == NULL)
1218                         i=0;
1219                 else
1220                         {
1221                         if ((rsa=RSAPrivateKey_dup(rsa)) == NULL)
1222                                 i=0;
1223                         }
1224                 if (!i)
1225                         {
1226                         SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_RSA_LIB);
1227                         return(0);
1228                         }
1229                 else
1230                         {
1231                         if (cert->rsa_tmp != NULL)
1232                                 RSA_free(cert->rsa_tmp);
1233                         cert->rsa_tmp=rsa;
1234                         return(1);
1235                         }
1236                 }
1237                 /* break; */
1238         case SSL_CTRL_SET_TMP_RSA_CB:
1239                 {
1240                 SSLerr(SSL_F_SSL3_CTX_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1241                 return(0);
1242                 }
1243                 break;
1244 #endif
1245 #ifndef OPENSSL_NO_DH
1246         case SSL_CTRL_SET_TMP_DH:
1247                 {
1248                 DH *new=NULL,*dh;
1249
1250                 dh=(DH *)parg;
1251                 if ((new=DHparams_dup(dh)) == NULL)
1252                         {
1253                         SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_DH_LIB);
1254                         return 0;
1255                         }
1256                 if (!(ctx->options & SSL_OP_SINGLE_DH_USE))
1257                         {
1258                         if (!DH_generate_key(new))
1259                                 {
1260                                 SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_DH_LIB);
1261                                 DH_free(new);
1262                                 return 0;
1263                                 }
1264                         }
1265                 if (cert->dh_tmp != NULL)
1266                         DH_free(cert->dh_tmp);
1267                 cert->dh_tmp=new;
1268                 return 1;
1269                 }
1270                 /*break; */
1271         case SSL_CTRL_SET_TMP_DH_CB:
1272                 {
1273                 SSLerr(SSL_F_SSL3_CTX_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1274                 return(0);
1275                 }
1276                 break;
1277 #endif
1278         /* A Thawte special :-) */
1279         case SSL_CTRL_EXTRA_CHAIN_CERT:
1280                 if (ctx->extra_certs == NULL)
1281                         {
1282                         if ((ctx->extra_certs=sk_X509_new_null()) == NULL)
1283                                 return(0);
1284                         }
1285                 sk_X509_push(ctx->extra_certs,(X509 *)parg);
1286                 break;
1287
1288         default:
1289                 return(0);
1290                 }
1291         return(1);
1292         }
1293
1294 long ssl3_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)())
1295         {
1296         CERT *cert;
1297
1298         cert=ctx->cert;
1299
1300         switch (cmd)
1301                 {
1302 #ifndef OPENSSL_NO_RSA
1303         case SSL_CTRL_SET_TMP_RSA_CB:
1304                 {
1305                 cert->rsa_tmp_cb = (RSA *(*)(SSL *, int, int))fp;
1306                 }
1307                 break;
1308 #endif
1309 #ifndef OPENSSL_NO_DH
1310         case SSL_CTRL_SET_TMP_DH_CB:
1311                 {
1312                 cert->dh_tmp_cb = (DH *(*)(SSL *, int, int))fp;
1313                 }
1314                 break;
1315 #endif
1316         default:
1317                 return(0);
1318                 }
1319         return(1);
1320         }
1321
1322 /* This function needs to check if the ciphers required are actually
1323  * available */
1324 SSL_CIPHER *ssl3_get_cipher_by_char(const unsigned char *p)
1325         {
1326         static int init=1;
1327         static SSL_CIPHER *sorted[SSL3_NUM_CIPHERS];
1328         SSL_CIPHER c,*cp= &c,**cpp;
1329         unsigned long id;
1330         int i;
1331
1332         if (init)
1333                 {
1334                 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
1335
1336                 for (i=0; i<SSL3_NUM_CIPHERS; i++)
1337                         sorted[i]= &(ssl3_ciphers[i]);
1338
1339                 qsort(  (char *)sorted,
1340                         SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
1341                         FP_ICC ssl_cipher_ptr_id_cmp);
1342
1343                 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
1344
1345                 init=0;
1346                 }
1347
1348         id=0x03000000L|((unsigned long)p[0]<<8L)|(unsigned long)p[1];
1349         c.id=id;
1350         cpp=(SSL_CIPHER **)OBJ_bsearch((char *)&cp,
1351                 (char *)sorted,
1352                 SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
1353                 FP_ICC ssl_cipher_ptr_id_cmp);
1354         if ((cpp == NULL) || !(*cpp)->valid)
1355                 return(NULL);
1356         else
1357                 return(*cpp);
1358         }
1359
1360 int ssl3_put_cipher_by_char(const SSL_CIPHER *c, unsigned char *p)
1361         {
1362         long l;
1363
1364         if (p != NULL)
1365                 {
1366                 l=c->id;
1367                 if ((l & 0xff000000) != 0x03000000) return(0);
1368                 p[0]=((unsigned char)(l>> 8L))&0xFF;
1369                 p[1]=((unsigned char)(l     ))&0xFF;
1370                 }
1371         return(2);
1372         }
1373
1374 SSL_CIPHER *ssl3_choose_cipher(SSL *s, STACK_OF(SSL_CIPHER) *clnt,
1375              STACK_OF(SSL_CIPHER) *srvr)
1376         {
1377         SSL_CIPHER *c,*ret=NULL;
1378         STACK_OF(SSL_CIPHER) *prio, *allow;
1379         int i,j,ok;
1380         CERT *cert;
1381         unsigned long alg,mask,emask;
1382
1383         /* Let's see which ciphers we can support */
1384         cert=s->cert;
1385
1386 #if 0
1387         /* Do not set the compare functions, because this may lead to a
1388          * reordering by "id". We want to keep the original ordering.
1389          * We may pay a price in performance during sk_SSL_CIPHER_find(),
1390          * but would have to pay with the price of sk_SSL_CIPHER_dup().
1391          */
1392         sk_SSL_CIPHER_set_cmp_func(srvr, ssl_cipher_ptr_id_cmp);
1393         sk_SSL_CIPHER_set_cmp_func(clnt, ssl_cipher_ptr_id_cmp);
1394 #endif
1395
1396 #ifdef CIPHER_DEBUG
1397         printf("Server has %d from %p:\n", sk_SSL_CIPHER_num(srvr), srvr);
1398         for(i=0 ; i < sk_SSL_CIPHER_num(srvr) ; ++i)
1399             {
1400             c=sk_SSL_CIPHER_value(srvr,i);
1401             printf("%p:%s\n",c,c->name);
1402             }
1403         printf("Client sent %d from %p:\n", sk_SSL_CIPHER_num(clnt), clnt);
1404         for(i=0 ; i < sk_SSL_CIPHER_num(clnt) ; ++i)
1405             {
1406             c=sk_SSL_CIPHER_value(clnt,i);
1407             printf("%p:%s\n",c,c->name);
1408             }
1409 #endif
1410
1411         if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
1412             {
1413             prio = srvr;
1414             allow = clnt;
1415             }
1416         else
1417             {
1418             prio = clnt;
1419             allow = srvr;
1420             }
1421
1422         for (i=0; i<sk_SSL_CIPHER_num(prio); i++)
1423                 {
1424                 c=sk_SSL_CIPHER_value(prio,i);
1425
1426                 ssl_set_cert_masks(cert,c);
1427                 mask=cert->mask;
1428                 emask=cert->export_mask;
1429                         
1430 #ifdef KSSL_DEBUG
1431                 printf("ssl3_choose_cipher %d alg= %lx\n", i,c->algorithms);
1432 #endif    /* KSSL_DEBUG */
1433
1434                 alg=c->algorithms&(SSL_MKEY_MASK|SSL_AUTH_MASK);
1435 #ifndef OPENSSL_NO_KRB5
1436                 if (alg & SSL_KRB5) 
1437                         {
1438                         if ( !kssl_keytab_is_available(s->kssl_ctx) )
1439                             continue;
1440                         }
1441 #endif /* OPENSSL_NO_KRB5 */
1442                 if (SSL_C_IS_EXPORT(c))
1443                         {
1444                         ok=((alg & emask) == alg)?1:0;
1445 #ifdef CIPHER_DEBUG
1446                         printf("%d:[%08lX:%08lX]%p:%s (export)\n",ok,alg,emask,
1447                                c,c->name);
1448 #endif
1449                         }
1450                 else
1451                         {
1452                         ok=((alg & mask) == alg)?1:0;
1453 #ifdef CIPHER_DEBUG
1454                         printf("%d:[%08lX:%08lX]%p:%s\n",ok,alg,mask,c,
1455                                c->name);
1456 #endif
1457                         }
1458
1459                 if (!ok) continue;
1460         
1461                 j=sk_SSL_CIPHER_find(allow,c);
1462                 if (j >= 0)
1463                         {
1464                         ret=sk_SSL_CIPHER_value(allow,j);
1465                         break;
1466                         }
1467                 }
1468         return(ret);
1469         }
1470
1471 int ssl3_get_req_cert_type(SSL *s, unsigned char *p)
1472         {
1473         int ret=0;
1474         unsigned long alg;
1475
1476         alg=s->s3->tmp.new_cipher->algorithms;
1477
1478 #ifndef OPENSSL_NO_DH
1479         if (alg & (SSL_kDHr|SSL_kEDH))
1480                 {
1481 #  ifndef OPENSSL_NO_RSA
1482                 p[ret++]=SSL3_CT_RSA_FIXED_DH;
1483 #  endif
1484 #  ifndef OPENSSL_NO_DSA
1485                 p[ret++]=SSL3_CT_DSS_FIXED_DH;
1486 #  endif
1487                 }
1488         if ((s->version == SSL3_VERSION) &&
1489                 (alg & (SSL_kEDH|SSL_kDHd|SSL_kDHr)))
1490                 {
1491 #  ifndef OPENSSL_NO_RSA
1492                 p[ret++]=SSL3_CT_RSA_EPHEMERAL_DH;
1493 #  endif
1494 #  ifndef OPENSSL_NO_DSA
1495                 p[ret++]=SSL3_CT_DSS_EPHEMERAL_DH;
1496 #  endif
1497                 }
1498 #endif /* !OPENSSL_NO_DH */
1499 #ifndef OPENSSL_NO_RSA
1500         p[ret++]=SSL3_CT_RSA_SIGN;
1501 #endif
1502 #ifndef OPENSSL_NO_DSA
1503         p[ret++]=SSL3_CT_DSS_SIGN;
1504 #endif
1505         return(ret);
1506         }
1507
1508 int ssl3_shutdown(SSL *s)
1509         {
1510
1511         /* Don't do anything much if we have not done the handshake or
1512          * we don't want to send messages :-) */
1513         if ((s->quiet_shutdown) || (s->state == SSL_ST_BEFORE))
1514                 {
1515                 s->shutdown=(SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
1516                 return(1);
1517                 }
1518
1519         if (!(s->shutdown & SSL_SENT_SHUTDOWN))
1520                 {
1521                 s->shutdown|=SSL_SENT_SHUTDOWN;
1522 #if 1
1523                 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_CLOSE_NOTIFY);
1524 #endif
1525                 /* our shutdown alert has been sent now, and if it still needs
1526                  * to be written, s->s3->alert_dispatch will be true */
1527                 }
1528         else if (s->s3->alert_dispatch)
1529                 {
1530                 /* resend it if not sent */
1531 #if 1
1532                 ssl3_dispatch_alert(s);
1533 #endif
1534                 }
1535         else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN))
1536                 {
1537                 /* If we are waiting for a close from our peer, we are closed */
1538                 ssl3_read_bytes(s,0,NULL,0,0);
1539                 }
1540
1541         if ((s->shutdown == (SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN)) &&
1542                 !s->s3->alert_dispatch)
1543                 return(1);
1544         else
1545                 return(0);
1546         }
1547
1548 int ssl3_write(SSL *s, const void *buf, int len)
1549         {
1550         int ret,n;
1551
1552 #if 0
1553         if (s->shutdown & SSL_SEND_SHUTDOWN)
1554                 {
1555                 s->rwstate=SSL_NOTHING;
1556                 return(0);
1557                 }
1558 #endif
1559         clear_sys_error();
1560         if (s->s3->renegotiate) ssl3_renegotiate_check(s);
1561
1562         /* This is an experimental flag that sends the
1563          * last handshake message in the same packet as the first
1564          * use data - used to see if it helps the TCP protocol during
1565          * session-id reuse */
1566         /* The second test is because the buffer may have been removed */
1567         if ((s->s3->flags & SSL3_FLAGS_POP_BUFFER) && (s->wbio == s->bbio))
1568                 {
1569                 /* First time through, we write into the buffer */
1570                 if (s->s3->delay_buf_pop_ret == 0)
1571                         {
1572                         ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA,
1573                                              buf,len);
1574                         if (ret <= 0) return(ret);
1575
1576                         s->s3->delay_buf_pop_ret=ret;
1577                         }
1578
1579                 s->rwstate=SSL_WRITING;
1580                 n=BIO_flush(s->wbio);
1581                 if (n <= 0) return(n);
1582                 s->rwstate=SSL_NOTHING;
1583
1584                 /* We have flushed the buffer, so remove it */
1585                 ssl_free_wbio_buffer(s);
1586                 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
1587
1588                 ret=s->s3->delay_buf_pop_ret;
1589                 s->s3->delay_buf_pop_ret=0;
1590                 }
1591         else
1592                 {
1593                 ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA,
1594                                      buf,len);
1595                 if (ret <= 0) return(ret);
1596                 }
1597
1598         return(ret);
1599         }
1600
1601 static int ssl3_read_internal(SSL *s, void *buf, int len, int peek)
1602         {
1603         int ret;
1604         
1605         clear_sys_error();
1606         if (s->s3->renegotiate) ssl3_renegotiate_check(s);
1607         s->s3->in_read_app_data=1;
1608         ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len,peek);
1609         if ((ret == -1) && (s->s3->in_read_app_data == 0))
1610                 {
1611                 /* ssl3_read_bytes decided to call s->handshake_func, which
1612                  * called ssl3_read_bytes to read handshake data.
1613                  * However, ssl3_read_bytes actually found application data
1614                  * and thinks that application data makes sense here (signalled
1615                  * by resetting 'in_read_app_data', strangely); so disable
1616                  * handshake processing and try to read application data again. */
1617                 s->in_handshake++;
1618                 ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len,peek);
1619                 s->in_handshake--;
1620                 }
1621         else
1622                 s->s3->in_read_app_data=0;
1623
1624         return(ret);
1625         }
1626
1627 int ssl3_read(SSL *s, void *buf, int len)
1628         {
1629         return ssl3_read_internal(s, buf, len, 0);
1630         }
1631
1632 int ssl3_peek(SSL *s, void *buf, int len)
1633         {
1634         return ssl3_read_internal(s, buf, len, 1);
1635         }
1636
1637 int ssl3_renegotiate(SSL *s)
1638         {
1639         if (s->handshake_func == NULL)
1640                 return(1);
1641
1642         if (s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)
1643                 return(0);
1644
1645         s->s3->renegotiate=1;
1646         return(1);
1647         }
1648
1649 int ssl3_renegotiate_check(SSL *s)
1650         {
1651         int ret=0;
1652
1653         if (s->s3->renegotiate)
1654                 {
1655                 if (    (s->s3->rbuf.left == 0) &&
1656                         (s->s3->wbuf.left == 0) &&
1657                         !SSL_in_init(s))
1658                         {
1659 /*
1660 if we are the server, and we have sent a 'RENEGOTIATE' message, we
1661 need to go to SSL_ST_ACCEPT.
1662 */
1663                         /* SSL_ST_ACCEPT */
1664                         s->state=SSL_ST_RENEGOTIATE;
1665                         s->s3->renegotiate=0;
1666                         s->s3->num_renegotiations++;
1667                         s->s3->total_renegotiations++;
1668                         ret=1;
1669                         }
1670                 }
1671         return(ret);
1672         }
1673