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