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