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