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