6c208ccfc1c3687703cfe1ed294d773f62811bca
[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  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113  *
114  * Portions of the attached software ("Contribution") are developed by 
115  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
116  *
117  * The Contribution is licensed pursuant to the OpenSSL open source
118  * license provided above.
119  *
120  * ECC cipher suite support in OpenSSL originally written by
121  * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
122  *
123  */
124
125 #include <stdio.h>
126 #include <openssl/objects.h>
127 #include "ssl_locl.h"
128 #include "kssl_lcl.h"
129 #include <openssl/md5.h>
130
131 const char *ssl3_version_str="SSLv3" OPENSSL_VERSION_PTEXT;
132
133 #define SSL3_NUM_CIPHERS        (sizeof(ssl3_ciphers)/sizeof(SSL_CIPHER))
134
135 static long ssl3_default_timeout(void );
136
137 OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
138 /* The RSA ciphers */
139 /* Cipher 01 */
140         {
141         1,
142         SSL3_TXT_RSA_NULL_MD5,
143         SSL3_CK_RSA_NULL_MD5,
144         SSL_kRSA|SSL_aRSA|SSL_eNULL |SSL_MD5|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 /* Cipher 02 */
153         {
154         1,
155         SSL3_TXT_RSA_NULL_SHA,
156         SSL3_CK_RSA_NULL_SHA,
157         SSL_kRSA|SSL_aRSA|SSL_eNULL |SSL_SHA1|SSL_SSLV3,
158         SSL_NOT_EXP|SSL_STRONG_NONE,
159         0,
160         0,
161         0,
162         SSL_ALL_CIPHERS,
163         SSL_ALL_STRENGTHS,
164         },
165
166 /* anon DH */
167 /* Cipher 17 */
168         {
169         1,
170         SSL3_TXT_ADH_RC4_40_MD5,
171         SSL3_CK_ADH_RC4_40_MD5,
172         SSL_kEDH |SSL_aNULL|SSL_RC4  |SSL_MD5 |SSL_SSLV3,
173         SSL_EXPORT|SSL_EXP40,
174         0,
175         40,
176         128,
177         SSL_ALL_CIPHERS,
178         SSL_ALL_STRENGTHS,
179         },
180 /* Cipher 18 */
181         {
182         1,
183         SSL3_TXT_ADH_RC4_128_MD5,
184         SSL3_CK_ADH_RC4_128_MD5,
185         SSL_kEDH |SSL_aNULL|SSL_RC4  |SSL_MD5 |SSL_SSLV3,
186         SSL_NOT_EXP|SSL_MEDIUM,
187         0,
188         128,
189         128,
190         SSL_ALL_CIPHERS,
191         SSL_ALL_STRENGTHS,
192         },
193 /* Cipher 19 */
194         {
195         1,
196         SSL3_TXT_ADH_DES_40_CBC_SHA,
197         SSL3_CK_ADH_DES_40_CBC_SHA,
198         SSL_kEDH |SSL_aNULL|SSL_DES|SSL_SHA1|SSL_SSLV3,
199         SSL_EXPORT|SSL_EXP40,
200         0,
201         40,
202         128,
203         SSL_ALL_CIPHERS,
204         SSL_ALL_STRENGTHS,
205         },
206 /* Cipher 1A */
207         {
208         1,
209         SSL3_TXT_ADH_DES_64_CBC_SHA,
210         SSL3_CK_ADH_DES_64_CBC_SHA,
211         SSL_kEDH |SSL_aNULL|SSL_DES  |SSL_SHA1|SSL_SSLV3,
212         SSL_NOT_EXP|SSL_LOW,
213         0,
214         56,
215         56,
216         SSL_ALL_CIPHERS,
217         SSL_ALL_STRENGTHS,
218         },
219 /* Cipher 1B */
220         {
221         1,
222         SSL3_TXT_ADH_DES_192_CBC_SHA,
223         SSL3_CK_ADH_DES_192_CBC_SHA,
224         SSL_kEDH |SSL_aNULL|SSL_3DES |SSL_SHA1|SSL_SSLV3,
225         SSL_NOT_EXP|SSL_HIGH,
226         0,
227         168,
228         168,
229         SSL_ALL_CIPHERS,
230         SSL_ALL_STRENGTHS,
231         },
232
233 /* RSA again */
234 /* Cipher 03 */
235         {
236         1,
237         SSL3_TXT_RSA_RC4_40_MD5,
238         SSL3_CK_RSA_RC4_40_MD5,
239         SSL_kRSA|SSL_aRSA|SSL_RC4  |SSL_MD5 |SSL_SSLV3,
240         SSL_EXPORT|SSL_EXP40,
241         0,
242         40,
243         128,
244         SSL_ALL_CIPHERS,
245         SSL_ALL_STRENGTHS,
246         },
247 /* Cipher 04 */
248         {
249         1,
250         SSL3_TXT_RSA_RC4_128_MD5,
251         SSL3_CK_RSA_RC4_128_MD5,
252         SSL_kRSA|SSL_aRSA|SSL_RC4  |SSL_MD5|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 05 */
261         {
262         1,
263         SSL3_TXT_RSA_RC4_128_SHA,
264         SSL3_CK_RSA_RC4_128_SHA,
265         SSL_kRSA|SSL_aRSA|SSL_RC4  |SSL_SHA1|SSL_SSLV3,
266         SSL_NOT_EXP|SSL_MEDIUM,
267         0,
268         128,
269         128,
270         SSL_ALL_CIPHERS,
271         SSL_ALL_STRENGTHS,
272         },
273 /* Cipher 06 */
274         {
275         1,
276         SSL3_TXT_RSA_RC2_40_MD5,
277         SSL3_CK_RSA_RC2_40_MD5,
278         SSL_kRSA|SSL_aRSA|SSL_RC2  |SSL_MD5 |SSL_SSLV3,
279         SSL_EXPORT|SSL_EXP40,
280         0,
281         40,
282         128,
283         SSL_ALL_CIPHERS,
284         SSL_ALL_STRENGTHS,
285         },
286 /* Cipher 07 */
287         {
288         1,
289         SSL3_TXT_RSA_IDEA_128_SHA,
290         SSL3_CK_RSA_IDEA_128_SHA,
291         SSL_kRSA|SSL_aRSA|SSL_IDEA |SSL_SHA1|SSL_SSLV3,
292         SSL_NOT_EXP|SSL_MEDIUM,
293         0,
294         128,
295         128,
296         SSL_ALL_CIPHERS,
297         SSL_ALL_STRENGTHS,
298         },
299 /* Cipher 08 */
300         {
301         1,
302         SSL3_TXT_RSA_DES_40_CBC_SHA,
303         SSL3_CK_RSA_DES_40_CBC_SHA,
304         SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA1|SSL_SSLV3,
305         SSL_EXPORT|SSL_EXP40,
306         0,
307         40,
308         56,
309         SSL_ALL_CIPHERS,
310         SSL_ALL_STRENGTHS,
311         },
312 /* Cipher 09 */
313         {
314         1,
315         SSL3_TXT_RSA_DES_64_CBC_SHA,
316         SSL3_CK_RSA_DES_64_CBC_SHA,
317         SSL_kRSA|SSL_aRSA|SSL_DES  |SSL_SHA1|SSL_SSLV3,
318         SSL_NOT_EXP|SSL_LOW,
319         0,
320         56,
321         56,
322         SSL_ALL_CIPHERS,
323         SSL_ALL_STRENGTHS,
324         },
325 /* Cipher 0A */
326         {
327         1,
328         SSL3_TXT_RSA_DES_192_CBC3_SHA,
329         SSL3_CK_RSA_DES_192_CBC3_SHA,
330         SSL_kRSA|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_SSLV3,
331         SSL_NOT_EXP|SSL_HIGH,
332         0,
333         168,
334         168,
335         SSL_ALL_CIPHERS,
336         SSL_ALL_STRENGTHS,
337         },
338
339 /*  The DH ciphers */
340 /* Cipher 0B */
341         {
342         0,
343         SSL3_TXT_DH_DSS_DES_40_CBC_SHA,
344         SSL3_CK_DH_DSS_DES_40_CBC_SHA,
345         SSL_kDHd |SSL_aDH|SSL_DES|SSL_SHA1|SSL_SSLV3,
346         SSL_EXPORT|SSL_EXP40,
347         0,
348         40,
349         56,
350         SSL_ALL_CIPHERS,
351         SSL_ALL_STRENGTHS,
352         },
353 /* Cipher 0C */
354         {
355         0,
356         SSL3_TXT_DH_DSS_DES_64_CBC_SHA,
357         SSL3_CK_DH_DSS_DES_64_CBC_SHA,
358         SSL_kDHd |SSL_aDH|SSL_DES  |SSL_SHA1|SSL_SSLV3,
359         SSL_NOT_EXP|SSL_LOW,
360         0,
361         56,
362         56,
363         SSL_ALL_CIPHERS,
364         SSL_ALL_STRENGTHS,
365         },
366 /* Cipher 0D */
367         {
368         0,
369         SSL3_TXT_DH_DSS_DES_192_CBC3_SHA,
370         SSL3_CK_DH_DSS_DES_192_CBC3_SHA,
371         SSL_kDHd |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_SSLV3,
372         SSL_NOT_EXP|SSL_HIGH,
373         0,
374         168,
375         168,
376         SSL_ALL_CIPHERS,
377         SSL_ALL_STRENGTHS,
378         },
379 /* Cipher 0E */
380         {
381         0,
382         SSL3_TXT_DH_RSA_DES_40_CBC_SHA,
383         SSL3_CK_DH_RSA_DES_40_CBC_SHA,
384         SSL_kDHr |SSL_aDH|SSL_DES|SSL_SHA1|SSL_SSLV3,
385         SSL_EXPORT|SSL_EXP40,
386         0,
387         40,
388         56,
389         SSL_ALL_CIPHERS,
390         SSL_ALL_STRENGTHS,
391         },
392 /* Cipher 0F */
393         {
394         0,
395         SSL3_TXT_DH_RSA_DES_64_CBC_SHA,
396         SSL3_CK_DH_RSA_DES_64_CBC_SHA,
397         SSL_kDHr |SSL_aDH|SSL_DES  |SSL_SHA1|SSL_SSLV3,
398         SSL_NOT_EXP|SSL_LOW,
399         0,
400         56,
401         56,
402         SSL_ALL_CIPHERS,
403         SSL_ALL_STRENGTHS,
404         },
405 /* Cipher 10 */
406         {
407         0,
408         SSL3_TXT_DH_RSA_DES_192_CBC3_SHA,
409         SSL3_CK_DH_RSA_DES_192_CBC3_SHA,
410         SSL_kDHr |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_SSLV3,
411         SSL_NOT_EXP|SSL_HIGH,
412         0,
413         168,
414         168,
415         SSL_ALL_CIPHERS,
416         SSL_ALL_STRENGTHS,
417         },
418
419 /* The Ephemeral DH ciphers */
420 /* Cipher 11 */
421         {
422         1,
423         SSL3_TXT_EDH_DSS_DES_40_CBC_SHA,
424         SSL3_CK_EDH_DSS_DES_40_CBC_SHA,
425         SSL_kEDH|SSL_aDSS|SSL_DES|SSL_SHA1|SSL_SSLV3,
426         SSL_EXPORT|SSL_EXP40,
427         0,
428         40,
429         56,
430         SSL_ALL_CIPHERS,
431         SSL_ALL_STRENGTHS,
432         },
433 /* Cipher 12 */
434         {
435         1,
436         SSL3_TXT_EDH_DSS_DES_64_CBC_SHA,
437         SSL3_CK_EDH_DSS_DES_64_CBC_SHA,
438         SSL_kEDH|SSL_aDSS|SSL_DES  |SSL_SHA1|SSL_SSLV3,
439         SSL_NOT_EXP|SSL_LOW,
440         0,
441         56,
442         56,
443         SSL_ALL_CIPHERS,
444         SSL_ALL_STRENGTHS,
445         },
446 /* Cipher 13 */
447         {
448         1,
449         SSL3_TXT_EDH_DSS_DES_192_CBC3_SHA,
450         SSL3_CK_EDH_DSS_DES_192_CBC3_SHA,
451         SSL_kEDH|SSL_aDSS|SSL_3DES |SSL_SHA1|SSL_SSLV3,
452         SSL_NOT_EXP|SSL_HIGH,
453         0,
454         168,
455         168,
456         SSL_ALL_CIPHERS,
457         SSL_ALL_STRENGTHS,
458         },
459 /* Cipher 14 */
460         {
461         1,
462         SSL3_TXT_EDH_RSA_DES_40_CBC_SHA,
463         SSL3_CK_EDH_RSA_DES_40_CBC_SHA,
464         SSL_kEDH|SSL_aRSA|SSL_DES|SSL_SHA1|SSL_SSLV3,
465         SSL_EXPORT|SSL_EXP40,
466         0,
467         40,
468         56,
469         SSL_ALL_CIPHERS,
470         SSL_ALL_STRENGTHS,
471         },
472 /* Cipher 15 */
473         {
474         1,
475         SSL3_TXT_EDH_RSA_DES_64_CBC_SHA,
476         SSL3_CK_EDH_RSA_DES_64_CBC_SHA,
477         SSL_kEDH|SSL_aRSA|SSL_DES  |SSL_SHA1|SSL_SSLV3,
478         SSL_NOT_EXP|SSL_LOW,
479         0,
480         56,
481         56,
482         SSL_ALL_CIPHERS,
483         SSL_ALL_STRENGTHS,
484         },
485 /* Cipher 16 */
486         {
487         1,
488         SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA,
489         SSL3_CK_EDH_RSA_DES_192_CBC3_SHA,
490         SSL_kEDH|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_SSLV3,
491         SSL_NOT_EXP|SSL_HIGH,
492         0,
493         168,
494         168,
495         SSL_ALL_CIPHERS,
496         SSL_ALL_STRENGTHS,
497         },
498
499 /* Fortezza */
500 /* Cipher 1C */
501         {
502         0,
503         SSL3_TXT_FZA_DMS_NULL_SHA,
504         SSL3_CK_FZA_DMS_NULL_SHA,
505         SSL_kFZA|SSL_aFZA |SSL_eNULL |SSL_SHA1|SSL_SSLV3,
506         SSL_NOT_EXP|SSL_STRONG_NONE,
507         0,
508         0,
509         0,
510         SSL_ALL_CIPHERS,
511         SSL_ALL_STRENGTHS,
512         },
513
514 /* Cipher 1D */
515         {
516         0,
517         SSL3_TXT_FZA_DMS_FZA_SHA,
518         SSL3_CK_FZA_DMS_FZA_SHA,
519         SSL_kFZA|SSL_aFZA |SSL_eFZA |SSL_SHA1|SSL_SSLV3,
520         SSL_NOT_EXP|SSL_STRONG_NONE,
521         0,
522         0,
523         0,
524         SSL_ALL_CIPHERS,
525         SSL_ALL_STRENGTHS,
526         },
527
528 #if 0
529 /* Cipher 1E */
530         {
531         0,
532         SSL3_TXT_FZA_DMS_RC4_SHA,
533         SSL3_CK_FZA_DMS_RC4_SHA,
534         SSL_kFZA|SSL_aFZA |SSL_RC4  |SSL_SHA1|SSL_SSLV3,
535         SSL_NOT_EXP|SSL_MEDIUM,
536         0,
537         128,
538         128,
539         SSL_ALL_CIPHERS,
540         SSL_ALL_STRENGTHS,
541         },
542 #endif
543
544 #ifndef OPENSSL_NO_KRB5
545 /* The Kerberos ciphers
546 ** 20000107 VRS: And the first shall be last,
547 ** in hopes of avoiding the lynx ssl renegotiation problem.
548 */
549 /* Cipher 1E VRS */
550         {
551         1,
552         SSL3_TXT_KRB5_DES_64_CBC_SHA,
553         SSL3_CK_KRB5_DES_64_CBC_SHA,
554         SSL_kKRB5|SSL_aKRB5|  SSL_DES|SSL_SHA1   |SSL_SSLV3,
555         SSL_NOT_EXP|SSL_LOW,
556         0,
557         56,
558         56,
559         SSL_ALL_CIPHERS,
560         SSL_ALL_STRENGTHS,
561         },
562
563 /* Cipher 1F VRS */
564         {
565         1,
566         SSL3_TXT_KRB5_DES_192_CBC3_SHA,
567         SSL3_CK_KRB5_DES_192_CBC3_SHA,
568         SSL_kKRB5|SSL_aKRB5|  SSL_3DES|SSL_SHA1  |SSL_SSLV3,
569         SSL_NOT_EXP|SSL_HIGH,
570         0,
571         112,
572         168,
573         SSL_ALL_CIPHERS,
574         SSL_ALL_STRENGTHS,
575         },
576
577 /* Cipher 20 VRS */
578         {
579         1,
580         SSL3_TXT_KRB5_RC4_128_SHA,
581         SSL3_CK_KRB5_RC4_128_SHA,
582         SSL_kKRB5|SSL_aKRB5|  SSL_RC4|SSL_SHA1  |SSL_SSLV3,
583         SSL_NOT_EXP|SSL_MEDIUM,
584         0,
585         128,
586         128,
587         SSL_ALL_CIPHERS,
588         SSL_ALL_STRENGTHS,
589         },
590
591 /* Cipher 21 VRS */
592         {
593         1,
594         SSL3_TXT_KRB5_IDEA_128_CBC_SHA,
595         SSL3_CK_KRB5_IDEA_128_CBC_SHA,
596         SSL_kKRB5|SSL_aKRB5|  SSL_IDEA|SSL_SHA1  |SSL_SSLV3,
597         SSL_NOT_EXP|SSL_MEDIUM,
598         0,
599         128,
600         128,
601         SSL_ALL_CIPHERS,
602         SSL_ALL_STRENGTHS,
603         },
604
605 /* Cipher 22 VRS */
606         {
607         1,
608         SSL3_TXT_KRB5_DES_64_CBC_MD5,
609         SSL3_CK_KRB5_DES_64_CBC_MD5,
610         SSL_kKRB5|SSL_aKRB5|  SSL_DES|SSL_MD5    |SSL_SSLV3,
611         SSL_NOT_EXP|SSL_LOW,
612         0,
613         56,
614         56,
615         SSL_ALL_CIPHERS,
616         SSL_ALL_STRENGTHS,
617         },
618
619 /* Cipher 23 VRS */
620         {
621         1,
622         SSL3_TXT_KRB5_DES_192_CBC3_MD5,
623         SSL3_CK_KRB5_DES_192_CBC3_MD5,
624         SSL_kKRB5|SSL_aKRB5|  SSL_3DES|SSL_MD5   |SSL_SSLV3,
625         SSL_NOT_EXP|SSL_HIGH,
626         0,
627         112,
628         168,
629         SSL_ALL_CIPHERS,
630         SSL_ALL_STRENGTHS,
631         },
632
633 /* Cipher 24 VRS */
634         {
635         1,
636         SSL3_TXT_KRB5_RC4_128_MD5,
637         SSL3_CK_KRB5_RC4_128_MD5,
638         SSL_kKRB5|SSL_aKRB5|  SSL_RC4|SSL_MD5  |SSL_SSLV3,
639         SSL_NOT_EXP|SSL_MEDIUM,
640         0,
641         128,
642         128,
643         SSL_ALL_CIPHERS,
644         SSL_ALL_STRENGTHS,
645         },
646
647 /* Cipher 25 VRS */
648         {
649         1,
650         SSL3_TXT_KRB5_IDEA_128_CBC_MD5,
651         SSL3_CK_KRB5_IDEA_128_CBC_MD5,
652         SSL_kKRB5|SSL_aKRB5|  SSL_IDEA|SSL_MD5  |SSL_SSLV3,
653         SSL_NOT_EXP|SSL_MEDIUM,
654         0,
655         128,
656         128,
657         SSL_ALL_CIPHERS,
658         SSL_ALL_STRENGTHS,
659         },
660
661 /* Cipher 26 VRS */
662         {
663         1,
664         SSL3_TXT_KRB5_DES_40_CBC_SHA,
665         SSL3_CK_KRB5_DES_40_CBC_SHA,
666         SSL_kKRB5|SSL_aKRB5|  SSL_DES|SSL_SHA1   |SSL_SSLV3,
667         SSL_EXPORT|SSL_EXP40,
668         0,
669         40,
670         56,
671         SSL_ALL_CIPHERS,
672         SSL_ALL_STRENGTHS,
673         },
674
675 /* Cipher 27 VRS */
676         {
677         1,
678         SSL3_TXT_KRB5_RC2_40_CBC_SHA,
679         SSL3_CK_KRB5_RC2_40_CBC_SHA,
680         SSL_kKRB5|SSL_aKRB5|  SSL_RC2|SSL_SHA1   |SSL_SSLV3,
681         SSL_EXPORT|SSL_EXP40,
682         0,
683         40,
684         128,
685         SSL_ALL_CIPHERS,
686         SSL_ALL_STRENGTHS,
687         },
688
689 /* Cipher 28 VRS */
690         {
691         1,
692         SSL3_TXT_KRB5_RC4_40_SHA,
693         SSL3_CK_KRB5_RC4_40_SHA,
694         SSL_kKRB5|SSL_aKRB5|  SSL_RC4|SSL_SHA1   |SSL_SSLV3,
695         SSL_EXPORT|SSL_EXP40,
696         0,
697         128,
698         128,
699         SSL_ALL_CIPHERS,
700         SSL_ALL_STRENGTHS,
701         },
702
703 /* Cipher 29 VRS */
704         {
705         1,
706         SSL3_TXT_KRB5_DES_40_CBC_MD5,
707         SSL3_CK_KRB5_DES_40_CBC_MD5,
708         SSL_kKRB5|SSL_aKRB5|  SSL_DES|SSL_MD5    |SSL_SSLV3,
709         SSL_EXPORT|SSL_EXP40,
710         0,
711         40,
712         56,
713         SSL_ALL_CIPHERS,
714         SSL_ALL_STRENGTHS,
715         },
716
717 /* Cipher 2A VRS */
718         {
719         1,
720         SSL3_TXT_KRB5_RC2_40_CBC_MD5,
721         SSL3_CK_KRB5_RC2_40_CBC_MD5,
722         SSL_kKRB5|SSL_aKRB5|  SSL_RC2|SSL_MD5    |SSL_SSLV3,
723         SSL_EXPORT|SSL_EXP40,
724         0,
725         40,
726         128,
727         SSL_ALL_CIPHERS,
728         SSL_ALL_STRENGTHS,
729         },
730
731 /* Cipher 2B VRS */
732         {
733         1,
734         SSL3_TXT_KRB5_RC4_40_MD5,
735         SSL3_CK_KRB5_RC4_40_MD5,
736         SSL_kKRB5|SSL_aKRB5|  SSL_RC4|SSL_MD5    |SSL_SSLV3,
737         SSL_EXPORT|SSL_EXP40,
738         0,
739         128,
740         128,
741         SSL_ALL_CIPHERS,
742         SSL_ALL_STRENGTHS,
743         },
744 #endif  /* OPENSSL_NO_KRB5 */
745
746
747 #if TLS1_ALLOW_EXPERIMENTAL_CIPHERSUITES
748         /* New TLS Export CipherSuites */
749         /* Cipher 60 */
750             {
751             1,
752             TLS1_TXT_RSA_EXPORT1024_WITH_RC4_56_MD5,
753             TLS1_CK_RSA_EXPORT1024_WITH_RC4_56_MD5,
754             SSL_kRSA|SSL_aRSA|SSL_RC4|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 61 */
763             {
764             1,
765             TLS1_TXT_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5,
766             TLS1_CK_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5,
767             SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5|SSL_TLSV1,
768             SSL_EXPORT|SSL_EXP56,
769             0,
770             56,
771             128,
772             SSL_ALL_CIPHERS,
773             SSL_ALL_STRENGTHS,
774             },
775         /* Cipher 62 */
776             {
777             1,
778             TLS1_TXT_RSA_EXPORT1024_WITH_DES_CBC_SHA,
779             TLS1_CK_RSA_EXPORT1024_WITH_DES_CBC_SHA,
780             SSL_kRSA|SSL_aRSA|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 63 */
789             {
790             1,
791             TLS1_TXT_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA,
792             TLS1_CK_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA,
793             SSL_kEDH|SSL_aDSS|SSL_DES|SSL_SHA|SSL_TLSV1,
794             SSL_EXPORT|SSL_EXP56,
795             0,
796             56,
797             56,
798             SSL_ALL_CIPHERS,
799             SSL_ALL_STRENGTHS,
800             },
801         /* Cipher 64 */
802             {
803             1,
804             TLS1_TXT_RSA_EXPORT1024_WITH_RC4_56_SHA,
805             TLS1_CK_RSA_EXPORT1024_WITH_RC4_56_SHA,
806             SSL_kRSA|SSL_aRSA|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 65 */
815             {
816             1,
817             TLS1_TXT_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA,
818             TLS1_CK_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA,
819             SSL_kEDH|SSL_aDSS|SSL_RC4|SSL_SHA|SSL_TLSV1,
820             SSL_EXPORT|SSL_EXP56,
821             0,
822             56,
823             128,
824             SSL_ALL_CIPHERS,
825             SSL_ALL_STRENGTHS,
826             },
827         /* Cipher 66 */
828             {
829             1,
830             TLS1_TXT_DHE_DSS_WITH_RC4_128_SHA,
831             TLS1_CK_DHE_DSS_WITH_RC4_128_SHA,
832             SSL_kEDH|SSL_aDSS|SSL_RC4|SSL_SHA|SSL_TLSV1,
833             SSL_NOT_EXP|SSL_MEDIUM,
834             0,
835             128,
836             128,
837             SSL_ALL_CIPHERS,
838             SSL_ALL_STRENGTHS
839             },
840 #endif
841         /* New AES ciphersuites */
842
843         /* Cipher 2F */
844             {
845             1,
846             TLS1_TXT_RSA_WITH_AES_128_SHA,
847             TLS1_CK_RSA_WITH_AES_128_SHA,
848             SSL_kRSA|SSL_aRSA|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 30 */
857             {
858             0,
859             TLS1_TXT_DH_DSS_WITH_AES_128_SHA,
860             TLS1_CK_DH_DSS_WITH_AES_128_SHA,
861             SSL_kDHd|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 31 */
870             {
871             0,
872             TLS1_TXT_DH_RSA_WITH_AES_128_SHA,
873             TLS1_CK_DH_RSA_WITH_AES_128_SHA,
874             SSL_kDHr|SSL_aDH|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 32 */
883             {
884             1,
885             TLS1_TXT_DHE_DSS_WITH_AES_128_SHA,
886             TLS1_CK_DHE_DSS_WITH_AES_128_SHA,
887             SSL_kEDH|SSL_aDSS|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 33 */
896             {
897             1,
898             TLS1_TXT_DHE_RSA_WITH_AES_128_SHA,
899             TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
900             SSL_kEDH|SSL_aRSA|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         /* Cipher 34 */
909             {
910             1,
911             TLS1_TXT_ADH_WITH_AES_128_SHA,
912             TLS1_CK_ADH_WITH_AES_128_SHA,
913             SSL_kEDH|SSL_aNULL|SSL_AES|SSL_SHA|SSL_TLSV1,
914             SSL_NOT_EXP|SSL_MEDIUM,
915             0,
916             128,
917             128,
918             SSL_ALL_CIPHERS,
919             SSL_ALL_STRENGTHS,
920             },
921
922         /* Cipher 35 */
923             {
924             1,
925             TLS1_TXT_RSA_WITH_AES_256_SHA,
926             TLS1_CK_RSA_WITH_AES_256_SHA,
927             SSL_kRSA|SSL_aRSA|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 36 */
936             {
937             0,
938             TLS1_TXT_DH_DSS_WITH_AES_256_SHA,
939             TLS1_CK_DH_DSS_WITH_AES_256_SHA,
940             SSL_kDHd|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 37 */
949             {
950             0,
951             TLS1_TXT_DH_RSA_WITH_AES_256_SHA,
952             TLS1_CK_DH_RSA_WITH_AES_256_SHA,
953             SSL_kDHr|SSL_aDH|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 38 */
962             {
963             1,
964             TLS1_TXT_DHE_DSS_WITH_AES_256_SHA,
965             TLS1_CK_DHE_DSS_WITH_AES_256_SHA,
966             SSL_kEDH|SSL_aDSS|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 39 */
975             {
976             1,
977             TLS1_TXT_DHE_RSA_WITH_AES_256_SHA,
978             TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
979             SSL_kEDH|SSL_aRSA|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         /* Cipher 3A */
988             {
989             1,
990             TLS1_TXT_ADH_WITH_AES_256_SHA,
991             TLS1_CK_ADH_WITH_AES_256_SHA,
992             SSL_kEDH|SSL_aNULL|SSL_AES|SSL_SHA|SSL_TLSV1,
993             SSL_NOT_EXP|SSL_HIGH,
994             0,
995             256,
996             256,
997             SSL_ALL_CIPHERS,
998             SSL_ALL_STRENGTHS,
999             },
1000
1001 #ifndef OPENSSL_NO_ECDH
1002         /* Cipher 47 */
1003             {
1004             1,
1005             TLS1_TXT_ECDH_ECDSA_WITH_NULL_SHA,
1006             TLS1_CK_ECDH_ECDSA_WITH_NULL_SHA,
1007             SSL_kECDH|SSL_aECDSA|SSL_eNULL|SSL_SHA|SSL_TLSV1,
1008             SSL_NOT_EXP,
1009             0,
1010             0,
1011             0,
1012             SSL_ALL_CIPHERS,
1013             SSL_ALL_STRENGTHS,
1014             },
1015
1016         /* Cipher 48 */
1017             {
1018             1,
1019             TLS1_TXT_ECDH_ECDSA_WITH_RC4_128_SHA,
1020             TLS1_CK_ECDH_ECDSA_WITH_RC4_128_SHA,
1021             SSL_kECDH|SSL_aECDSA|SSL_RC4|SSL_SHA|SSL_TLSV1,
1022             SSL_NOT_EXP,
1023             0,
1024             128,
1025             128,
1026             SSL_ALL_CIPHERS,
1027             SSL_ALL_STRENGTHS,
1028             },
1029
1030         /* Cipher 49 */
1031             {
1032             1,
1033             TLS1_TXT_ECDH_ECDSA_WITH_DES_CBC_SHA,
1034             TLS1_CK_ECDH_ECDSA_WITH_DES_CBC_SHA,
1035             SSL_kECDH|SSL_aECDSA|SSL_DES|SSL_SHA|SSL_TLSV1,
1036             SSL_NOT_EXP|SSL_LOW,
1037             0,
1038             56,
1039             56,
1040             SSL_ALL_CIPHERS,
1041             SSL_ALL_STRENGTHS,
1042             },
1043
1044         /* Cipher 4A */
1045             {
1046             1,
1047             TLS1_TXT_ECDH_ECDSA_WITH_DES_192_CBC3_SHA,
1048             TLS1_CK_ECDH_ECDSA_WITH_DES_192_CBC3_SHA,
1049             SSL_kECDH|SSL_aECDSA|SSL_3DES|SSL_SHA|SSL_TLSV1,
1050             SSL_NOT_EXP|SSL_HIGH,
1051             0,
1052             168,
1053             168,
1054             SSL_ALL_CIPHERS,
1055             SSL_ALL_STRENGTHS,
1056             },
1057
1058         /* Cipher 4B */
1059             {
1060             1,
1061             TLS1_TXT_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
1062             TLS1_CK_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
1063             SSL_kECDH|SSL_aECDSA|SSL_AES|SSL_SHA|SSL_TLSV1,
1064             SSL_NOT_EXP|SSL_MEDIUM,
1065             0,
1066             128,
1067             128,
1068             SSL_ALL_CIPHERS,
1069             SSL_ALL_STRENGTHS,
1070             },
1071
1072         /* Cipher 4C */
1073             {
1074             1,
1075             TLS1_TXT_ECDH_ECDSA_WITH_AES_256_CBC_SHA,
1076             TLS1_CK_ECDH_ECDSA_WITH_AES_256_CBC_SHA,
1077             SSL_kECDH|SSL_aECDSA|SSL_AES|SSL_SHA|SSL_TLSV1,
1078             SSL_NOT_EXP|SSL_HIGH,
1079             0,
1080             256,
1081             256,
1082             SSL_ALL_CIPHERS,
1083             SSL_ALL_STRENGTHS,
1084             },
1085
1086         /* Cipher 5B */
1087         /* XXX NOTE: The ECC/TLS draft has a bug and reuses 4B for this */
1088             {
1089             1,
1090             TLS1_TXT_ECDH_ECDSA_EXPORT_WITH_RC4_40_SHA,
1091             TLS1_CK_ECDH_ECDSA_EXPORT_WITH_RC4_40_SHA,
1092             SSL_kECDH|SSL_aECDSA|SSL_RC4|SSL_SHA|SSL_TLSV1,
1093             SSL_EXPORT|SSL_EXP40,
1094             0,
1095             40,
1096             128,
1097             SSL_ALL_CIPHERS,
1098             SSL_ALL_STRENGTHS,
1099             },
1100
1101         /* Cipher 5C */
1102         /* XXX NOTE: The ECC/TLS draft has a bug and reuses 4C for this */
1103             {
1104             1,
1105             TLS1_TXT_ECDH_ECDSA_EXPORT_WITH_RC4_56_SHA,
1106             TLS1_CK_ECDH_ECDSA_EXPORT_WITH_RC4_56_SHA,
1107             SSL_kECDH|SSL_aECDSA|SSL_RC4|SSL_SHA|SSL_TLSV1,
1108             SSL_EXPORT|SSL_EXP56,
1109             0,
1110             56,
1111             128,
1112             SSL_ALL_CIPHERS,
1113             SSL_ALL_STRENGTHS,
1114             },
1115
1116         /* Cipher 4D */
1117             {
1118             1,
1119             TLS1_TXT_ECDH_RSA_WITH_NULL_SHA,
1120             TLS1_CK_ECDH_RSA_WITH_NULL_SHA,
1121             SSL_kECDH|SSL_aRSA|SSL_eNULL|SSL_SHA|SSL_TLSV1,
1122             SSL_NOT_EXP,
1123             0,
1124             0,
1125             0,
1126             SSL_ALL_CIPHERS,
1127             SSL_ALL_STRENGTHS,
1128             },
1129
1130         /* Cipher 4E */
1131             {
1132             1,
1133             TLS1_TXT_ECDH_RSA_WITH_RC4_128_SHA,
1134             TLS1_CK_ECDH_RSA_WITH_RC4_128_SHA,
1135             SSL_kECDH|SSL_aRSA|SSL_RC4|SSL_SHA|SSL_TLSV1,
1136             SSL_NOT_EXP,
1137             0,
1138             128,
1139             128,
1140             SSL_ALL_CIPHERS,
1141             SSL_ALL_STRENGTHS,
1142             },
1143
1144         /* Cipher 4F */
1145             {
1146             1,
1147             TLS1_TXT_ECDH_RSA_WITH_DES_CBC_SHA,
1148             TLS1_CK_ECDH_RSA_WITH_DES_CBC_SHA,
1149             SSL_kECDH|SSL_aRSA|SSL_DES|SSL_SHA|SSL_TLSV1,
1150             SSL_NOT_EXP|SSL_LOW,
1151             0,
1152             56,
1153             56,
1154             SSL_ALL_CIPHERS,
1155             SSL_ALL_STRENGTHS,
1156             },
1157
1158         /* Cipher 50 */
1159             {
1160             1,
1161             TLS1_TXT_ECDH_RSA_WITH_DES_192_CBC3_SHA,
1162             TLS1_CK_ECDH_RSA_WITH_DES_192_CBC3_SHA,
1163             SSL_kECDH|SSL_aRSA|SSL_3DES|SSL_SHA|SSL_TLSV1,
1164             SSL_NOT_EXP|SSL_HIGH,
1165             0,
1166             168,
1167             168,
1168             SSL_ALL_CIPHERS,
1169             SSL_ALL_STRENGTHS,
1170             },
1171
1172         /* Cipher 51 */
1173             {
1174             1,
1175             TLS1_TXT_ECDH_RSA_WITH_AES_128_CBC_SHA,
1176             TLS1_CK_ECDH_RSA_WITH_AES_128_CBC_SHA,
1177             SSL_kECDH|SSL_aRSA|SSL_AES|SSL_SHA|SSL_TLSV1,
1178             SSL_NOT_EXP|SSL_MEDIUM,
1179             0,
1180             128,
1181             128,
1182             SSL_ALL_CIPHERS,
1183             SSL_ALL_STRENGTHS,
1184             },
1185
1186         /* Cipher 52 */
1187             {
1188             1,
1189             TLS1_TXT_ECDH_RSA_WITH_AES_256_CBC_SHA,
1190             TLS1_CK_ECDH_RSA_WITH_AES_256_CBC_SHA,
1191             SSL_kECDH|SSL_aRSA|SSL_AES|SSL_SHA|SSL_TLSV1,
1192             SSL_NOT_EXP|SSL_HIGH,
1193             0,
1194             256,
1195             256,
1196             SSL_ALL_CIPHERS,
1197             SSL_ALL_STRENGTHS,
1198             },
1199
1200         /* Cipher 53 */
1201             {
1202             1,
1203             TLS1_TXT_ECDH_RSA_EXPORT_WITH_RC4_40_SHA,
1204             TLS1_CK_ECDH_RSA_EXPORT_WITH_RC4_40_SHA,
1205             SSL_kECDH|SSL_aRSA|SSL_RC4|SSL_SHA|SSL_TLSV1,
1206             SSL_EXPORT|SSL_EXP40,
1207             0,
1208             40,
1209             128,
1210             SSL_ALL_CIPHERS,
1211             SSL_ALL_STRENGTHS,
1212             },
1213
1214         /* Cipher 54 */
1215             {
1216             1,
1217             TLS1_TXT_ECDH_RSA_EXPORT_WITH_RC4_56_SHA,
1218             TLS1_CK_ECDH_RSA_EXPORT_WITH_RC4_56_SHA,
1219             SSL_kECDH|SSL_aRSA|SSL_RC4|SSL_SHA|SSL_TLSV1,
1220             SSL_EXPORT|SSL_EXP56,
1221             0,
1222             56,
1223             128,
1224             SSL_ALL_CIPHERS,
1225             SSL_ALL_STRENGTHS,
1226             },
1227
1228         /* Cipher 55 */
1229             {
1230             1,
1231             TLS1_TXT_ECDH_anon_WITH_NULL_SHA,
1232             TLS1_CK_ECDH_anon_WITH_NULL_SHA,
1233             SSL_kECDHE|SSL_aNULL|SSL_eNULL|SSL_SHA|SSL_TLSV1,
1234             SSL_NOT_EXP,
1235             0,
1236             0,
1237             0,
1238             SSL_ALL_CIPHERS,
1239             SSL_ALL_STRENGTHS,
1240             },
1241
1242         /* Cipher 56 */
1243             {
1244             1,
1245             TLS1_TXT_ECDH_anon_WITH_RC4_128_SHA,
1246             TLS1_CK_ECDH_anon_WITH_RC4_128_SHA,
1247             SSL_kECDHE|SSL_aNULL|SSL_RC4|SSL_SHA|SSL_TLSV1,
1248             SSL_NOT_EXP,
1249             0,
1250             128,
1251             128,
1252             SSL_ALL_CIPHERS,
1253             SSL_ALL_STRENGTHS,
1254             },
1255
1256         /* Cipher 57 */
1257             {
1258             1,
1259             TLS1_TXT_ECDH_anon_WITH_DES_CBC_SHA,
1260             TLS1_CK_ECDH_anon_WITH_DES_CBC_SHA,
1261             SSL_kECDHE|SSL_aNULL|SSL_DES|SSL_SHA|SSL_TLSV1,
1262             SSL_NOT_EXP|SSL_LOW,
1263             0,
1264             56,
1265             56,
1266             SSL_ALL_CIPHERS,
1267             SSL_ALL_STRENGTHS,
1268             },
1269
1270         /* Cipher 58 */
1271             {
1272             1,
1273             TLS1_TXT_ECDH_anon_WITH_DES_192_CBC3_SHA,
1274             TLS1_CK_ECDH_anon_WITH_DES_192_CBC3_SHA,
1275             SSL_kECDHE|SSL_aNULL|SSL_3DES|SSL_SHA|SSL_TLSV1,
1276             SSL_NOT_EXP|SSL_HIGH,
1277             0,
1278             168,
1279             168,
1280             SSL_ALL_CIPHERS,
1281             SSL_ALL_STRENGTHS,
1282             },
1283
1284         /* Cipher 59 */
1285             {
1286             1,
1287             TLS1_TXT_ECDH_anon_EXPORT_WITH_DES_40_CBC_SHA,
1288             TLS1_CK_ECDH_anon_EXPORT_WITH_DES_40_CBC_SHA,
1289             SSL_kECDHE|SSL_aNULL|SSL_DES|SSL_SHA|SSL_TLSV1,
1290             SSL_EXPORT|SSL_EXP40,
1291             0,
1292             40,
1293             56,
1294             SSL_ALL_CIPHERS,
1295             SSL_ALL_STRENGTHS,
1296             },
1297
1298         /* Cipher 5A */
1299             {
1300             1,
1301             TLS1_TXT_ECDH_anon_EXPORT_WITH_RC4_40_SHA,
1302             TLS1_CK_ECDH_anon_EXPORT_WITH_RC4_40_SHA,
1303             SSL_kECDHE|SSL_aNULL|SSL_RC4|SSL_SHA|SSL_TLSV1,
1304             SSL_EXPORT|SSL_EXP40,
1305             0,
1306             40,
1307             128,
1308             SSL_ALL_CIPHERS,
1309             SSL_ALL_STRENGTHS,
1310             },
1311
1312         /* Cipher 77 XXX: ECC ciphersuites offering forward secrecy
1313          * are not yet specified in the ECC/TLS draft but our code
1314          * allows them to be implemented very easily. To add such
1315          * a cipher suite, one needs to add two constant definitions
1316          * to tls1.h and a new structure in this file as shown below. We 
1317          * illustrate the process for the made-up cipher
1318          * ECDHE-ECDSA-AES128-SHA.
1319          */
1320             {
1321             1,
1322             TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
1323             TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
1324             SSL_kECDHE|SSL_aECDSA|SSL_AES|SSL_SHA|SSL_TLSV1,
1325             SSL_NOT_EXP|SSL_MEDIUM,
1326             0,
1327             128,
1328             128,
1329             SSL_ALL_CIPHERS,
1330             SSL_ALL_STRENGTHS,
1331             },
1332
1333         /* Cipher 78 XXX: Another made-up ECC cipher suite that
1334          * offers forward secrecy (ECDHE-RSA-AES128-SHA).
1335          */
1336             {
1337             1,
1338             TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
1339             TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
1340             SSL_kECDHE|SSL_aRSA|SSL_AES|SSL_SHA|SSL_TLSV1,
1341             SSL_NOT_EXP|SSL_MEDIUM,
1342             0,
1343             128,
1344             128,
1345             SSL_ALL_CIPHERS,
1346             SSL_ALL_STRENGTHS,
1347             },
1348
1349 #endif /* !OPENSSL_NO_ECDH */
1350
1351 /* end of list */
1352         };
1353
1354 static SSL3_ENC_METHOD SSLv3_enc_data={
1355         ssl3_enc,
1356         ssl3_mac,
1357         ssl3_setup_key_block,
1358         ssl3_generate_master_secret,
1359         ssl3_change_cipher_state,
1360         ssl3_final_finish_mac,
1361         MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
1362         ssl3_cert_verify_mac,
1363         SSL3_MD_CLIENT_FINISHED_CONST,4,
1364         SSL3_MD_SERVER_FINISHED_CONST,4,
1365         ssl3_alert_code,
1366         };
1367
1368 static SSL_METHOD SSLv3_data= {
1369         SSL3_VERSION,
1370         ssl3_new,
1371         ssl3_clear,
1372         ssl3_free,
1373         ssl_undefined_function,
1374         ssl_undefined_function,
1375         ssl3_read,
1376         ssl3_peek,
1377         ssl3_write,
1378         ssl3_shutdown,
1379         ssl3_renegotiate,
1380         ssl3_renegotiate_check,
1381         ssl3_ctrl,
1382         ssl3_ctx_ctrl,
1383         ssl3_get_cipher_by_char,
1384         ssl3_put_cipher_by_char,
1385         ssl3_pending,
1386         ssl3_num_ciphers,
1387         ssl3_get_cipher,
1388         ssl_bad_method,
1389         ssl3_default_timeout,
1390         &SSLv3_enc_data,
1391         ssl_undefined_function,
1392         ssl3_callback_ctrl,
1393         ssl3_ctx_callback_ctrl,
1394         };
1395
1396 static long ssl3_default_timeout(void)
1397         {
1398         /* 2 hours, the 24 hours mentioned in the SSLv3 spec
1399          * is way too long for http, the cache would over fill */
1400         return(60*60*2);
1401         }
1402
1403 SSL_METHOD *sslv3_base_method(void)
1404         {
1405         return(&SSLv3_data);
1406         }
1407
1408 int ssl3_num_ciphers(void)
1409         {
1410         return(SSL3_NUM_CIPHERS);
1411         }
1412
1413 SSL_CIPHER *ssl3_get_cipher(unsigned int u)
1414         {
1415         if (u < SSL3_NUM_CIPHERS)
1416                 return(&(ssl3_ciphers[SSL3_NUM_CIPHERS-1-u]));
1417         else
1418                 return(NULL);
1419         }
1420
1421 int ssl3_pending(SSL *s)
1422         {
1423         if (s->rstate == SSL_ST_READ_BODY)
1424                 return 0;
1425         
1426         return (s->s3->rrec.type == SSL3_RT_APPLICATION_DATA) ? s->s3->rrec.length : 0;
1427         }
1428
1429 int ssl3_new(SSL *s)
1430         {
1431         SSL3_STATE *s3;
1432
1433         if ((s3=OPENSSL_malloc(sizeof *s3)) == NULL) goto err;
1434         memset(s3,0,sizeof *s3);
1435         EVP_MD_CTX_init(&s3->finish_dgst1);
1436         EVP_MD_CTX_init(&s3->finish_dgst2);
1437
1438         s->s3=s3;
1439
1440         s->method->ssl_clear(s);
1441         return(1);
1442 err:
1443         return(0);
1444         }
1445
1446 void ssl3_free(SSL *s)
1447         {
1448         if(s == NULL)
1449             return;
1450
1451         ssl3_cleanup_key_block(s);
1452         if (s->s3->rbuf.buf != NULL)
1453                 OPENSSL_free(s->s3->rbuf.buf);
1454         if (s->s3->wbuf.buf != NULL)
1455                 OPENSSL_free(s->s3->wbuf.buf);
1456         if (s->s3->rrec.comp != NULL)
1457                 OPENSSL_free(s->s3->rrec.comp);
1458 #ifndef OPENSSL_NO_DH
1459         if (s->s3->tmp.dh != NULL)
1460                 DH_free(s->s3->tmp.dh);
1461 #endif
1462 #ifndef OPENSSL_NO_ECDH
1463         if (s->s3->tmp.ecdh != NULL)
1464                 EC_KEY_free(s->s3->tmp.ecdh);
1465 #endif
1466
1467         if (s->s3->tmp.ca_names != NULL)
1468                 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1469         EVP_MD_CTX_cleanup(&s->s3->finish_dgst1);
1470         EVP_MD_CTX_cleanup(&s->s3->finish_dgst2);
1471         OPENSSL_cleanse(s->s3,sizeof *s->s3);
1472         OPENSSL_free(s->s3);
1473         s->s3=NULL;
1474         }
1475
1476 void ssl3_clear(SSL *s)
1477         {
1478         unsigned char *rp,*wp;
1479         size_t rlen, wlen;
1480
1481         ssl3_cleanup_key_block(s);
1482         if (s->s3->tmp.ca_names != NULL)
1483                 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1484
1485         if (s->s3->rrec.comp != NULL)
1486                 {
1487                 OPENSSL_free(s->s3->rrec.comp);
1488                 s->s3->rrec.comp=NULL;
1489                 }
1490 #ifndef OPENSSL_NO_DH
1491         if (s->s3->tmp.dh != NULL)
1492                 DH_free(s->s3->tmp.dh);
1493 #endif
1494 #ifndef OPENSSL_NO_ECDH
1495         if (s->s3->tmp.ecdh != NULL)
1496                 EC_KEY_free(s->s3->tmp.ecdh);
1497 #endif
1498
1499         rp = s->s3->rbuf.buf;
1500         wp = s->s3->wbuf.buf;
1501         rlen = s->s3->rbuf.len;
1502         wlen = s->s3->wbuf.len;
1503
1504         EVP_MD_CTX_cleanup(&s->s3->finish_dgst1);
1505         EVP_MD_CTX_cleanup(&s->s3->finish_dgst2);
1506
1507         memset(s->s3,0,sizeof *s->s3);
1508         s->s3->rbuf.buf = rp;
1509         s->s3->wbuf.buf = wp;
1510         s->s3->rbuf.len = rlen;
1511         s->s3->wbuf.len = wlen;
1512
1513         ssl_free_wbio_buffer(s);
1514
1515         s->packet_length=0;
1516         s->s3->renegotiate=0;
1517         s->s3->total_renegotiations=0;
1518         s->s3->num_renegotiations=0;
1519         s->s3->in_read_app_data=0;
1520         s->version=SSL3_VERSION;
1521         }
1522
1523 long ssl3_ctrl(SSL *s, int cmd, long larg, void *parg)
1524         {
1525         int ret=0;
1526
1527 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_RSA)
1528         if (
1529 #ifndef OPENSSL_NO_RSA
1530             cmd == SSL_CTRL_SET_TMP_RSA ||
1531             cmd == SSL_CTRL_SET_TMP_RSA_CB ||
1532 #endif
1533 #ifndef OPENSSL_NO_DSA
1534             cmd == SSL_CTRL_SET_TMP_DH ||
1535             cmd == SSL_CTRL_SET_TMP_DH_CB ||
1536 #endif
1537                 0)
1538                 {
1539                 if (!ssl_cert_inst(&s->cert))
1540                         {
1541                         SSLerr(SSL_F_SSL3_CTRL, ERR_R_MALLOC_FAILURE);
1542                         return(0);
1543                         }
1544                 }
1545 #endif
1546
1547         switch (cmd)
1548                 {
1549         case SSL_CTRL_GET_SESSION_REUSED:
1550                 ret=s->hit;
1551                 break;
1552         case SSL_CTRL_GET_CLIENT_CERT_REQUEST:
1553                 break;
1554         case SSL_CTRL_GET_NUM_RENEGOTIATIONS:
1555                 ret=s->s3->num_renegotiations;
1556                 break;
1557         case SSL_CTRL_CLEAR_NUM_RENEGOTIATIONS:
1558                 ret=s->s3->num_renegotiations;
1559                 s->s3->num_renegotiations=0;
1560                 break;
1561         case SSL_CTRL_GET_TOTAL_RENEGOTIATIONS:
1562                 ret=s->s3->total_renegotiations;
1563                 break;
1564         case SSL_CTRL_GET_FLAGS:
1565                 ret=(int)(s->s3->flags);
1566                 break;
1567 #ifndef OPENSSL_NO_RSA
1568         case SSL_CTRL_NEED_TMP_RSA:
1569                 if ((s->cert != NULL) && (s->cert->rsa_tmp == NULL) &&
1570                     ((s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) ||
1571                      (EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8))))
1572                         ret = 1;
1573                 break;
1574         case SSL_CTRL_SET_TMP_RSA:
1575                 {
1576                         RSA *rsa = (RSA *)parg;
1577                         if (rsa == NULL)
1578                                 {
1579                                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
1580                                 return(ret);
1581                                 }
1582                         if ((rsa = RSAPrivateKey_dup(rsa)) == NULL)
1583                                 {
1584                                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_RSA_LIB);
1585                                 return(ret);
1586                                 }
1587                         if (s->cert->rsa_tmp != NULL)
1588                                 RSA_free(s->cert->rsa_tmp);
1589                         s->cert->rsa_tmp = rsa;
1590                         ret = 1;
1591                 }
1592                 break;
1593         case SSL_CTRL_SET_TMP_RSA_CB:
1594                 {
1595                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1596                 return(ret);
1597                 }
1598                 break;
1599 #endif
1600 #ifndef OPENSSL_NO_DH
1601         case SSL_CTRL_SET_TMP_DH:
1602                 {
1603                         DH *dh = (DH *)parg;
1604                         if (dh == NULL)
1605                                 {
1606                                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
1607                                 return(ret);
1608                                 }
1609                         if ((dh = DHparams_dup(dh)) == NULL)
1610                                 {
1611                                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_DH_LIB);
1612                                 return(ret);
1613                                 }
1614                         if (!(s->options & SSL_OP_SINGLE_DH_USE))
1615                                 {
1616                                 if (!DH_generate_key(dh))
1617                                         {
1618                                         DH_free(dh);
1619                                         SSLerr(SSL_F_SSL3_CTRL, ERR_R_DH_LIB);
1620                                         return(ret);
1621                                         }
1622                                 }
1623                         if (s->cert->dh_tmp != NULL)
1624                                 DH_free(s->cert->dh_tmp);
1625                         s->cert->dh_tmp = dh;
1626                         ret = 1;
1627                 }
1628                 break;
1629         case SSL_CTRL_SET_TMP_DH_CB:
1630                 {
1631                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1632                 return(ret);
1633                 }
1634                 break;
1635 #endif
1636 #ifndef OPENSSL_NO_ECDH
1637         case SSL_CTRL_SET_TMP_ECDH:
1638                 {
1639                 EC_KEY *ecdh = NULL;
1640                         
1641                 if (parg == NULL)
1642                         {
1643                         SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
1644                         return(ret);
1645                         }
1646                 if (!EC_KEY_up_ref((EC_KEY *)parg))
1647                         {
1648                         SSLerr(SSL_F_SSL3_CTRL,ERR_R_ECDH_LIB);
1649                         return(ret);
1650                         }
1651                 ecdh = (EC_KEY *)parg;
1652                 if (!(s->options & SSL_OP_SINGLE_ECDH_USE))
1653                         {
1654                         if (!EC_KEY_generate_key(ecdh))
1655                                 {
1656                                 EC_KEY_free(ecdh);
1657                                 SSLerr(SSL_F_SSL3_CTRL,ERR_R_ECDH_LIB);
1658                                 return(ret);
1659                                 }
1660                         }
1661                 if (s->cert->ecdh_tmp != NULL)
1662                         EC_KEY_free(s->cert->ecdh_tmp);
1663                 s->cert->ecdh_tmp = ecdh;
1664                 ret = 1;
1665                 }
1666                 break;
1667         case SSL_CTRL_SET_TMP_ECDH_CB:
1668                 {
1669                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1670                 return(ret);
1671                 }
1672                 break;
1673 #endif /* !OPENSSL_NO_ECDH */
1674         default:
1675                 break;
1676                 }
1677         return(ret);
1678         }
1679
1680 long ssl3_callback_ctrl(SSL *s, int cmd, void (*fp)())
1681         {
1682         int ret=0;
1683
1684 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_RSA)
1685         if (
1686 #ifndef OPENSSL_NO_RSA
1687             cmd == SSL_CTRL_SET_TMP_RSA_CB ||
1688 #endif
1689 #ifndef OPENSSL_NO_DSA
1690             cmd == SSL_CTRL_SET_TMP_DH_CB ||
1691 #endif
1692                 0)
1693                 {
1694                 if (!ssl_cert_inst(&s->cert))
1695                         {
1696                         SSLerr(SSL_F_SSL3_CALLBACK_CTRL, ERR_R_MALLOC_FAILURE);
1697                         return(0);
1698                         }
1699                 }
1700 #endif
1701
1702         switch (cmd)
1703                 {
1704 #ifndef OPENSSL_NO_RSA
1705         case SSL_CTRL_SET_TMP_RSA_CB:
1706                 {
1707                 s->cert->rsa_tmp_cb = (RSA *(*)(SSL *, int, int))fp;
1708                 }
1709                 break;
1710 #endif
1711 #ifndef OPENSSL_NO_DH
1712         case SSL_CTRL_SET_TMP_DH_CB:
1713                 {
1714                 s->cert->dh_tmp_cb = (DH *(*)(SSL *, int, int))fp;
1715                 }
1716                 break;
1717 #endif
1718 #ifndef OPENSSL_NO_ECDH
1719         case SSL_CTRL_SET_TMP_ECDH_CB:
1720                 {
1721                 s->cert->ecdh_tmp_cb = (EC_KEY *(*)(SSL *, int, int))fp;
1722                 }
1723                 break;
1724 #endif
1725         default:
1726                 break;
1727                 }
1728         return(ret);
1729         }
1730
1731 long ssl3_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
1732         {
1733         CERT *cert;
1734
1735         cert=ctx->cert;
1736
1737         switch (cmd)
1738                 {
1739 #ifndef OPENSSL_NO_RSA
1740         case SSL_CTRL_NEED_TMP_RSA:
1741                 if (    (cert->rsa_tmp == NULL) &&
1742                         ((cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) ||
1743                          (EVP_PKEY_size(cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8)))
1744                         )
1745                         return(1);
1746                 else
1747                         return(0);
1748                 /* break; */
1749         case SSL_CTRL_SET_TMP_RSA:
1750                 {
1751                 RSA *rsa;
1752                 int i;
1753
1754                 rsa=(RSA *)parg;
1755                 i=1;
1756                 if (rsa == NULL)
1757                         i=0;
1758                 else
1759                         {
1760                         if ((rsa=RSAPrivateKey_dup(rsa)) == NULL)
1761                                 i=0;
1762                         }
1763                 if (!i)
1764                         {
1765                         SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_RSA_LIB);
1766                         return(0);
1767                         }
1768                 else
1769                         {
1770                         if (cert->rsa_tmp != NULL)
1771                                 RSA_free(cert->rsa_tmp);
1772                         cert->rsa_tmp=rsa;
1773                         return(1);
1774                         }
1775                 }
1776                 /* break; */
1777         case SSL_CTRL_SET_TMP_RSA_CB:
1778                 {
1779                 SSLerr(SSL_F_SSL3_CTX_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1780                 return(0);
1781                 }
1782                 break;
1783 #endif
1784 #ifndef OPENSSL_NO_DH
1785         case SSL_CTRL_SET_TMP_DH:
1786                 {
1787                 DH *new=NULL,*dh;
1788
1789                 dh=(DH *)parg;
1790                 if ((new=DHparams_dup(dh)) == NULL)
1791                         {
1792                         SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_DH_LIB);
1793                         return 0;
1794                         }
1795                 if (!(ctx->options & SSL_OP_SINGLE_DH_USE))
1796                         {
1797                         if (!DH_generate_key(new))
1798                                 {
1799                                 SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_DH_LIB);
1800                                 DH_free(new);
1801                                 return 0;
1802                                 }
1803                         }
1804                 if (cert->dh_tmp != NULL)
1805                         DH_free(cert->dh_tmp);
1806                 cert->dh_tmp=new;
1807                 return 1;
1808                 }
1809                 /*break; */
1810         case SSL_CTRL_SET_TMP_DH_CB:
1811                 {
1812                 SSLerr(SSL_F_SSL3_CTX_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1813                 return(0);
1814                 }
1815                 break;
1816 #endif
1817 #ifndef OPENSSL_NO_ECDH
1818         case SSL_CTRL_SET_TMP_ECDH:
1819                 {
1820                 EC_KEY *ecdh = NULL;
1821                         
1822                 if (parg == NULL)
1823                         {
1824                         SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_ECDH_LIB);
1825                         return 0;
1826                         }
1827                 if (!EC_KEY_up_ref((EC_KEY *)parg))
1828                         {
1829                         SSLerr(SSL_F_SSL3_CTRL,ERR_R_ECDH_LIB);
1830                         return 0;
1831                         }
1832                 ecdh = (EC_KEY *)parg;
1833                 if (!(ctx->options & SSL_OP_SINGLE_ECDH_USE))
1834                         {
1835                         if (!EC_KEY_generate_key(ecdh))
1836                                 {
1837                                 EC_KEY_free(ecdh);
1838                                 SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_ECDH_LIB);
1839                                 return 0;
1840                                 }
1841                         }
1842
1843                 if (cert->ecdh_tmp != NULL)
1844                         {
1845                         EC_KEY_free(cert->ecdh_tmp);
1846                         }
1847                 cert->ecdh_tmp = ecdh;
1848                 return 1;
1849                 }
1850                 /* break; */
1851         case SSL_CTRL_SET_TMP_ECDH_CB:
1852                 {
1853                 SSLerr(SSL_F_SSL3_CTX_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1854                 return(0);
1855                 }
1856                 break;
1857 #endif /* !OPENSSL_NO_ECDH */
1858         /* A Thawte special :-) */
1859         case SSL_CTRL_EXTRA_CHAIN_CERT:
1860                 if (ctx->extra_certs == NULL)
1861                         {
1862                         if ((ctx->extra_certs=sk_X509_new_null()) == NULL)
1863                                 return(0);
1864                         }
1865                 sk_X509_push(ctx->extra_certs,(X509 *)parg);
1866                 break;
1867
1868         default:
1869                 return(0);
1870                 }
1871         return(1);
1872         }
1873
1874 long ssl3_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)())
1875         {
1876         CERT *cert;
1877
1878         cert=ctx->cert;
1879
1880         switch (cmd)
1881                 {
1882 #ifndef OPENSSL_NO_RSA
1883         case SSL_CTRL_SET_TMP_RSA_CB:
1884                 {
1885                 cert->rsa_tmp_cb = (RSA *(*)(SSL *, int, int))fp;
1886                 }
1887                 break;
1888 #endif
1889 #ifndef OPENSSL_NO_DH
1890         case SSL_CTRL_SET_TMP_DH_CB:
1891                 {
1892                 cert->dh_tmp_cb = (DH *(*)(SSL *, int, int))fp;
1893                 }
1894                 break;
1895 #endif
1896 #ifndef OPENSSL_NO_ECDH
1897         case SSL_CTRL_SET_TMP_ECDH_CB:
1898                 {
1899                 cert->ecdh_tmp_cb = (EC_KEY *(*)(SSL *, int, int))fp;
1900                 }
1901                 break;
1902 #endif
1903         default:
1904                 return(0);
1905                 }
1906         return(1);
1907         }
1908
1909 /* This function needs to check if the ciphers required are actually
1910  * available */
1911 SSL_CIPHER *ssl3_get_cipher_by_char(const unsigned char *p)
1912         {
1913         static int init=1;
1914         static SSL_CIPHER *sorted[SSL3_NUM_CIPHERS];
1915         SSL_CIPHER c,*cp= &c,**cpp;
1916         unsigned long id;
1917         unsigned int i;
1918
1919         if (init)
1920                 {
1921                 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
1922
1923                 if (init)
1924                         {
1925                         for (i=0; i<SSL3_NUM_CIPHERS; i++)
1926                                 sorted[i]= &(ssl3_ciphers[i]);
1927
1928                         qsort(sorted,
1929                                 SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
1930                                 FP_ICC ssl_cipher_ptr_id_cmp);
1931
1932                         init=0;
1933                         }
1934                 
1935                 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
1936                 }
1937
1938         id=0x03000000L|((unsigned long)p[0]<<8L)|(unsigned long)p[1];
1939         c.id=id;
1940         cpp=(SSL_CIPHER **)OBJ_bsearch((char *)&cp,
1941                 (char *)sorted,
1942                 SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
1943                 FP_ICC ssl_cipher_ptr_id_cmp);
1944         if ((cpp == NULL) || !(*cpp)->valid)
1945                 return(NULL);
1946         else
1947                 return(*cpp);
1948         }
1949
1950 int ssl3_put_cipher_by_char(const SSL_CIPHER *c, unsigned char *p)
1951         {
1952         long l;
1953
1954         if (p != NULL)
1955                 {
1956                 l=c->id;
1957                 if ((l & 0xff000000) != 0x03000000) return(0);
1958                 p[0]=((unsigned char)(l>> 8L))&0xFF;
1959                 p[1]=((unsigned char)(l     ))&0xFF;
1960                 }
1961         return(2);
1962         }
1963
1964 SSL_CIPHER *ssl3_choose_cipher(SSL *s, STACK_OF(SSL_CIPHER) *clnt,
1965              STACK_OF(SSL_CIPHER) *srvr)
1966         {
1967         SSL_CIPHER *c,*ret=NULL;
1968         STACK_OF(SSL_CIPHER) *prio, *allow;
1969         int i,j,ok;
1970         CERT *cert;
1971         unsigned long alg,mask,emask;
1972
1973         /* Let's see which ciphers we can support */
1974         cert=s->cert;
1975
1976 #if 0
1977         /* Do not set the compare functions, because this may lead to a
1978          * reordering by "id". We want to keep the original ordering.
1979          * We may pay a price in performance during sk_SSL_CIPHER_find(),
1980          * but would have to pay with the price of sk_SSL_CIPHER_dup().
1981          */
1982         sk_SSL_CIPHER_set_cmp_func(srvr, ssl_cipher_ptr_id_cmp);
1983         sk_SSL_CIPHER_set_cmp_func(clnt, ssl_cipher_ptr_id_cmp);
1984 #endif
1985
1986 #ifdef CIPHER_DEBUG
1987         printf("Server has %d from %p:\n", sk_SSL_CIPHER_num(srvr), srvr);
1988         for(i=0 ; i < sk_SSL_CIPHER_num(srvr) ; ++i)
1989             {
1990             c=sk_SSL_CIPHER_value(srvr,i);
1991             printf("%p:%s\n",c,c->name);
1992             }
1993         printf("Client sent %d from %p:\n", sk_SSL_CIPHER_num(clnt), clnt);
1994         for(i=0 ; i < sk_SSL_CIPHER_num(clnt) ; ++i)
1995             {
1996             c=sk_SSL_CIPHER_value(clnt,i);
1997             printf("%p:%s\n",c,c->name);
1998             }
1999 #endif
2000
2001         if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
2002             {
2003             prio = srvr;
2004             allow = clnt;
2005             }
2006         else
2007             {
2008             prio = clnt;
2009             allow = srvr;
2010             }
2011
2012         for (i=0; i<sk_SSL_CIPHER_num(prio); i++)
2013                 {
2014                 c=sk_SSL_CIPHER_value(prio,i);
2015
2016                 ssl_set_cert_masks(cert,c);
2017                 mask=cert->mask;
2018                 emask=cert->export_mask;
2019                         
2020 #ifdef KSSL_DEBUG
2021                 printf("ssl3_choose_cipher %d alg= %lx\n", i,c->algorithms);
2022 #endif    /* KSSL_DEBUG */
2023
2024                 alg=c->algorithms&(SSL_MKEY_MASK|SSL_AUTH_MASK);
2025 #ifndef OPENSSL_NO_KRB5
2026                 if (alg & SSL_KRB5) 
2027                         {
2028                         if ( !kssl_keytab_is_available(s->kssl_ctx) )
2029                             continue;
2030                         }
2031 #endif /* OPENSSL_NO_KRB5 */
2032                 if (SSL_C_IS_EXPORT(c))
2033                         {
2034                         ok=((alg & emask) == alg)?1:0;
2035 #ifdef CIPHER_DEBUG
2036                         printf("%d:[%08lX:%08lX]%p:%s (export)\n",ok,alg,emask,
2037                                c,c->name);
2038 #endif
2039                         }
2040                 else
2041                         {
2042                         ok=((alg & mask) == alg)?1:0;
2043 #ifdef CIPHER_DEBUG
2044                         printf("%d:[%08lX:%08lX]%p:%s\n",ok,alg,mask,c,
2045                                c->name);
2046 #endif
2047                         }
2048
2049                 if (!ok) continue;
2050                 j=sk_SSL_CIPHER_find(allow,c);
2051                 if (j >= 0)
2052                         {
2053                         ret=sk_SSL_CIPHER_value(allow,j);
2054                         break;
2055                         }
2056                 }
2057         return(ret);
2058         }
2059
2060 int ssl3_get_req_cert_type(SSL *s, unsigned char *p)
2061         {
2062         int ret=0;
2063         unsigned long alg;
2064
2065         alg=s->s3->tmp.new_cipher->algorithms;
2066
2067 #ifndef OPENSSL_NO_DH
2068         if (alg & (SSL_kDHr|SSL_kEDH))
2069                 {
2070 #  ifndef OPENSSL_NO_RSA
2071                 p[ret++]=SSL3_CT_RSA_FIXED_DH;
2072 #  endif
2073 #  ifndef OPENSSL_NO_DSA
2074                 p[ret++]=SSL3_CT_DSS_FIXED_DH;
2075 #  endif
2076                 }
2077         if ((s->version == SSL3_VERSION) &&
2078                 (alg & (SSL_kEDH|SSL_kDHd|SSL_kDHr)))
2079                 {
2080 #  ifndef OPENSSL_NO_RSA
2081                 p[ret++]=SSL3_CT_RSA_EPHEMERAL_DH;
2082 #  endif
2083 #  ifndef OPENSSL_NO_DSA
2084                 p[ret++]=SSL3_CT_DSS_EPHEMERAL_DH;
2085 #  endif
2086                 }
2087 #endif /* !OPENSSL_NO_DH */
2088 #ifndef OPENSSL_NO_RSA
2089         p[ret++]=SSL3_CT_RSA_SIGN;
2090 #endif
2091 #ifndef OPENSSL_NO_DSA
2092         p[ret++]=SSL3_CT_DSS_SIGN;
2093 #endif
2094 #ifndef OPENSSL_NO_ECDH
2095         /* We should ask for fixed ECDH certificates only
2096          * for SSL_kECDH (and not SSL_kECDHE)
2097          */
2098         if ((alg & SSL_kECDH) && (s->version >= TLS1_VERSION))
2099                 {
2100                 p[ret++]=TLS_CT_RSA_FIXED_ECDH;
2101                 p[ret++]=TLS_CT_ECDSA_FIXED_ECDH;
2102                 }
2103 #endif
2104
2105 #ifndef OPENSSL_NO_ECDSA
2106         /* ECDSA certs can be used with RSA cipher suites as well 
2107          * so we don't need to check for SSL_kECDH or SSL_kECDHE
2108          */
2109         if (s->version >= TLS1_VERSION)
2110                 {
2111                 p[ret++]=TLS_CT_ECDSA_SIGN;
2112                 }
2113 #endif  
2114         return(ret);
2115         }
2116
2117 int ssl3_shutdown(SSL *s)
2118         {
2119
2120         /* Don't do anything much if we have not done the handshake or
2121          * we don't want to send messages :-) */
2122         if ((s->quiet_shutdown) || (s->state == SSL_ST_BEFORE))
2123                 {
2124                 s->shutdown=(SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
2125                 return(1);
2126                 }
2127
2128         if (!(s->shutdown & SSL_SENT_SHUTDOWN))
2129                 {
2130                 s->shutdown|=SSL_SENT_SHUTDOWN;
2131 #if 1
2132                 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_CLOSE_NOTIFY);
2133 #endif
2134                 /* our shutdown alert has been sent now, and if it still needs
2135                  * to be written, s->s3->alert_dispatch will be true */
2136                 }
2137         else if (s->s3->alert_dispatch)
2138                 {
2139                 /* resend it if not sent */
2140 #if 1
2141                 ssl3_dispatch_alert(s);
2142 #endif
2143                 }
2144         else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN))
2145                 {
2146                 /* If we are waiting for a close from our peer, we are closed */
2147                 ssl3_read_bytes(s,0,NULL,0,0);
2148                 }
2149
2150         if ((s->shutdown == (SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN)) &&
2151                 !s->s3->alert_dispatch)
2152                 return(1);
2153         else
2154                 return(0);
2155         }
2156
2157 int ssl3_write(SSL *s, const void *buf, int len)
2158         {
2159         int ret,n;
2160
2161 #if 0
2162         if (s->shutdown & SSL_SEND_SHUTDOWN)
2163                 {
2164                 s->rwstate=SSL_NOTHING;
2165                 return(0);
2166                 }
2167 #endif
2168         clear_sys_error();
2169         if (s->s3->renegotiate) ssl3_renegotiate_check(s);
2170
2171         /* This is an experimental flag that sends the
2172          * last handshake message in the same packet as the first
2173          * use data - used to see if it helps the TCP protocol during
2174          * session-id reuse */
2175         /* The second test is because the buffer may have been removed */
2176         if ((s->s3->flags & SSL3_FLAGS_POP_BUFFER) && (s->wbio == s->bbio))
2177                 {
2178                 /* First time through, we write into the buffer */
2179                 if (s->s3->delay_buf_pop_ret == 0)
2180                         {
2181                         ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA,
2182                                              buf,len);
2183                         if (ret <= 0) return(ret);
2184
2185                         s->s3->delay_buf_pop_ret=ret;
2186                         }
2187
2188                 s->rwstate=SSL_WRITING;
2189                 n=BIO_flush(s->wbio);
2190                 if (n <= 0) return(n);
2191                 s->rwstate=SSL_NOTHING;
2192
2193                 /* We have flushed the buffer, so remove it */
2194                 ssl_free_wbio_buffer(s);
2195                 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
2196
2197                 ret=s->s3->delay_buf_pop_ret;
2198                 s->s3->delay_buf_pop_ret=0;
2199                 }
2200         else
2201                 {
2202                 ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA,
2203                                      buf,len);
2204                 if (ret <= 0) return(ret);
2205                 }
2206
2207         return(ret);
2208         }
2209
2210 static int ssl3_read_internal(SSL *s, void *buf, int len, int peek)
2211         {
2212         int ret;
2213         
2214         clear_sys_error();
2215         if (s->s3->renegotiate) ssl3_renegotiate_check(s);
2216         s->s3->in_read_app_data=1;
2217         ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len,peek);
2218         if ((ret == -1) && (s->s3->in_read_app_data == 2))
2219                 {
2220                 /* ssl3_read_bytes decided to call s->handshake_func, which
2221                  * called ssl3_read_bytes to read handshake data.
2222                  * However, ssl3_read_bytes actually found application data
2223                  * and thinks that application data makes sense here; so disable
2224                  * handshake processing and try to read application data again. */
2225                 s->in_handshake++;
2226                 ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len,peek);
2227                 s->in_handshake--;
2228                 }
2229         else
2230                 s->s3->in_read_app_data=0;
2231
2232         return(ret);
2233         }
2234
2235 int ssl3_read(SSL *s, void *buf, int len)
2236         {
2237         return ssl3_read_internal(s, buf, len, 0);
2238         }
2239
2240 int ssl3_peek(SSL *s, void *buf, int len)
2241         {
2242         return ssl3_read_internal(s, buf, len, 1);
2243         }
2244
2245 int ssl3_renegotiate(SSL *s)
2246         {
2247         if (s->handshake_func == NULL)
2248                 return(1);
2249
2250         if (s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)
2251                 return(0);
2252
2253         s->s3->renegotiate=1;
2254         return(1);
2255         }
2256
2257 int ssl3_renegotiate_check(SSL *s)
2258         {
2259         int ret=0;
2260
2261         if (s->s3->renegotiate)
2262                 {
2263                 if (    (s->s3->rbuf.left == 0) &&
2264                         (s->s3->wbuf.left == 0) &&
2265                         !SSL_in_init(s))
2266                         {
2267 /*
2268 if we are the server, and we have sent a 'RENEGOTIATE' message, we
2269 need to go to SSL_ST_ACCEPT.
2270 */
2271                         /* SSL_ST_ACCEPT */
2272                         s->state=SSL_ST_RENEGOTIATE;
2273                         s->s3->renegotiate=0;
2274                         s->s3->num_renegotiations++;
2275                         s->s3->total_renegotiations++;
2276                         ret=1;
2277                         }
2278                 }
2279         return(ret);
2280         }
2281