Undo a couple of kludges.
[openssl.git] / ssl / ssl_ciph.c
1 /* ssl/ssl_ciph.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 #include <stdio.h>
60 #include "objects.h"
61 #include "comp.h"
62 #include "ssl_locl.h"
63
64 #define SSL_ENC_DES_IDX         0
65 #define SSL_ENC_3DES_IDX        1
66 #define SSL_ENC_RC4_IDX         2
67 #define SSL_ENC_RC2_IDX         3
68 #define SSL_ENC_IDEA_IDX        4
69 #define SSL_ENC_eFZA_IDX        5
70 #define SSL_ENC_NULL_IDX        6
71 #define SSL_ENC_NUM_IDX         7
72
73 static EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX]={
74         NULL,NULL,NULL,NULL,NULL,NULL,
75         };
76
77 static STACK /* SSL_COMP */ *ssl_comp_methods=NULL;
78
79 #define SSL_MD_MD5_IDX  0
80 #define SSL_MD_SHA1_IDX 1
81 #define SSL_MD_NUM_IDX  2
82 static EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX]={
83         NULL,NULL,
84         };
85
86 typedef struct cipher_sort_st
87         {
88         SSL_CIPHER *cipher;
89         int pref;
90         } CIPHER_SORT;
91
92 #define CIPHER_ADD      1
93 #define CIPHER_KILL     2
94 #define CIPHER_DEL      3
95 #define CIPHER_ORD      4
96
97 typedef struct cipher_choice_st
98         {
99         int type;
100         unsigned long algorithms;
101         unsigned long mask;
102         long top;
103         } CIPHER_CHOICE;
104
105 typedef struct cipher_order_st
106         {
107         SSL_CIPHER *cipher;
108         int active;
109         int dead;
110         struct cipher_order_st *next,*prev;
111         } CIPHER_ORDER;
112
113 static SSL_CIPHER cipher_aliases[]={
114         {0,SSL_TXT_ALL, 0,SSL_ALL,   0,SSL_ALL},        /* must be first */
115         {0,SSL_TXT_kRSA,0,SSL_kRSA,  0,SSL_MKEY_MASK},
116         {0,SSL_TXT_kDHr,0,SSL_kDHr,  0,SSL_MKEY_MASK},
117         {0,SSL_TXT_kDHd,0,SSL_kDHd,  0,SSL_MKEY_MASK},
118         {0,SSL_TXT_kEDH,0,SSL_kEDH,  0,SSL_MKEY_MASK},
119         {0,SSL_TXT_kFZA,0,SSL_kFZA,  0,SSL_MKEY_MASK},
120         {0,SSL_TXT_DH,  0,SSL_DH,    0,SSL_MKEY_MASK},
121         {0,SSL_TXT_EDH, 0,SSL_EDH,   0,SSL_MKEY_MASK|SSL_AUTH_MASK},
122
123         {0,SSL_TXT_aRSA,0,SSL_aRSA,  0,SSL_AUTH_MASK},
124         {0,SSL_TXT_aDSS,0,SSL_aDSS,  0,SSL_AUTH_MASK},
125         {0,SSL_TXT_aFZA,0,SSL_aFZA,  0,SSL_AUTH_MASK},
126         {0,SSL_TXT_aNULL,0,SSL_aNULL,0,SSL_AUTH_MASK},
127         {0,SSL_TXT_aDH, 0,SSL_aDH,   0,SSL_AUTH_MASK},
128         {0,SSL_TXT_DSS, 0,SSL_DSS,   0,SSL_AUTH_MASK},
129
130         {0,SSL_TXT_DES, 0,SSL_DES,   0,SSL_ENC_MASK},
131         {0,SSL_TXT_3DES,0,SSL_3DES,  0,SSL_ENC_MASK},
132         {0,SSL_TXT_RC4, 0,SSL_RC4,   0,SSL_ENC_MASK},
133         {0,SSL_TXT_RC2, 0,SSL_RC2,   0,SSL_ENC_MASK},
134         {0,SSL_TXT_IDEA,0,SSL_IDEA,  0,SSL_ENC_MASK},
135         {0,SSL_TXT_eNULL,0,SSL_eNULL,0,SSL_ENC_MASK},
136         {0,SSL_TXT_eFZA,0,SSL_eFZA,  0,SSL_ENC_MASK},
137
138         {0,SSL_TXT_MD5, 0,SSL_MD5,   0,SSL_MAC_MASK},
139         {0,SSL_TXT_SHA1,0,SSL_SHA1,  0,SSL_MAC_MASK},
140         {0,SSL_TXT_SHA, 0,SSL_SHA,   0,SSL_MAC_MASK},
141
142         {0,SSL_TXT_NULL,0,SSL_NULL,  0,SSL_ENC_MASK},
143         {0,SSL_TXT_RSA, 0,SSL_RSA,   0,SSL_AUTH_MASK|SSL_MKEY_MASK},
144         {0,SSL_TXT_ADH, 0,SSL_ADH,   0,SSL_AUTH_MASK|SSL_MKEY_MASK},
145         {0,SSL_TXT_FZA, 0,SSL_FZA,   0,SSL_AUTH_MASK|SSL_MKEY_MASK|SSL_ENC_MASK},
146
147         {0,SSL_TXT_EXP40, 0,SSL_EXP40, 0,SSL_EXP_MASK},
148         {0,SSL_TXT_EXPORT,0,SSL_EXP40, 0,SSL_EXP_MASK},
149         {0,SSL_TXT_EXP56, 0,SSL_EXP56, 0,SSL_EXP_MASK},
150         {0,SSL_TXT_SSLV2, 0,SSL_SSLV2, 0,SSL_SSL_MASK},
151         {0,SSL_TXT_SSLV3, 0,SSL_SSLV3, 0,SSL_SSL_MASK},
152         {0,SSL_TXT_TLSV1, 0,SSL_TLSV1, 0,SSL_SSL_MASK},
153         {0,SSL_TXT_LOW,   0,SSL_LOW,   0,SSL_STRONG_MASK},
154         {0,SSL_TXT_MEDIUM,0,SSL_MEDIUM,0,SSL_STRONG_MASK},
155         {0,SSL_TXT_HIGH,  0,SSL_HIGH,  0,SSL_STRONG_MASK},
156         };
157
158 static int init_ciphers=1;
159 static void load_ciphers();
160
161 static int cmp_by_name(a,b)
162 SSL_CIPHER **a,**b;
163         {
164         return(strcmp((*a)->name,(*b)->name));
165         }
166
167 static void load_ciphers()
168         {
169         init_ciphers=0;
170         ssl_cipher_methods[SSL_ENC_DES_IDX]= 
171                 EVP_get_cipherbyname(SN_des_cbc);
172         ssl_cipher_methods[SSL_ENC_3DES_IDX]=
173                 EVP_get_cipherbyname(SN_des_ede3_cbc);
174         ssl_cipher_methods[SSL_ENC_RC4_IDX]=
175                 EVP_get_cipherbyname(SN_rc4);
176         ssl_cipher_methods[SSL_ENC_RC2_IDX]= 
177                 EVP_get_cipherbyname(SN_rc2_cbc);
178         ssl_cipher_methods[SSL_ENC_IDEA_IDX]= 
179                 EVP_get_cipherbyname(SN_idea_cbc);
180
181         ssl_digest_methods[SSL_MD_MD5_IDX]=
182                 EVP_get_digestbyname(SN_md5);
183         ssl_digest_methods[SSL_MD_SHA1_IDX]=
184                 EVP_get_digestbyname(SN_sha1);
185         }
186
187 int ssl_cipher_get_evp(s,enc,md,comp)
188 SSL_SESSION *s;
189 EVP_CIPHER **enc;
190 EVP_MD **md;
191 SSL_COMP **comp;
192         {
193         int i;
194         SSL_CIPHER *c;
195
196         c=s->cipher;
197         if (c == NULL) return(0);
198         if (comp != NULL)
199                 {
200                 SSL_COMP ctmp;
201
202                 if (s->compress_meth == 0)
203                         *comp=NULL;
204                 else if (ssl_comp_methods == NULL)
205                         {
206                         /* bad */
207                         *comp=NULL;
208                         }
209                 else
210                         {
211
212                         ctmp.id=s->compress_meth;
213                         i=sk_find(ssl_comp_methods,(char *)&ctmp);
214                         if (i >= 0)
215                                 *comp=(SSL_COMP *)sk_value(ssl_comp_methods,i);
216                         else
217                                 *comp=NULL;
218                         }
219                 }
220
221         if ((enc == NULL) || (md == NULL)) return(0);
222
223         switch (c->algorithms & SSL_ENC_MASK)
224                 {
225         case SSL_DES:
226                 i=SSL_ENC_DES_IDX;
227                 break;
228         case SSL_3DES:
229                 i=SSL_ENC_3DES_IDX;
230                 break;
231         case SSL_RC4:
232                 i=SSL_ENC_RC4_IDX;
233                 break;
234         case SSL_RC2:
235                 i=SSL_ENC_RC2_IDX;
236                 break;
237         case SSL_IDEA:
238                 i=SSL_ENC_IDEA_IDX;
239                 break;
240         case SSL_eNULL:
241                 i=SSL_ENC_NULL_IDX;
242                 break;
243         default:
244                 i= -1;
245                 break;
246                 }
247
248         if ((i < 0) || (i > SSL_ENC_NUM_IDX))
249                 *enc=NULL;
250         else
251                 {
252                 if (i == SSL_ENC_NULL_IDX)
253                         *enc=EVP_enc_null();
254                 else
255                         *enc=ssl_cipher_methods[i];
256                 }
257
258         switch (c->algorithms & SSL_MAC_MASK)
259                 {
260         case SSL_MD5:
261                 i=SSL_MD_MD5_IDX;
262                 break;
263         case SSL_SHA1:
264                 i=SSL_MD_SHA1_IDX;
265                 break;
266         default:
267                 i= -1;
268                 break;
269                 }
270         if ((i < 0) || (i > SSL_MD_NUM_IDX))
271                 *md=NULL;
272         else
273                 *md=ssl_digest_methods[i];
274
275         if ((*enc != NULL) && (*md != NULL))
276                 return(1);
277         else
278                 return(0);
279         }
280
281 #define ITEM_SEP(a) \
282         (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
283
284 static void ll_append_tail(head,curr,tail)
285 CIPHER_ORDER **head,*curr,**tail;
286         {
287         if (curr == *tail) return;
288         if (curr == *head)
289                 *head=curr->next;
290         if (curr->prev != NULL)
291                 curr->prev->next=curr->next;
292         if (curr->next != NULL) /* should always be true */
293                 curr->next->prev=curr->prev;
294         (*tail)->next=curr;
295         curr->prev= *tail;
296         curr->next=NULL;
297         *tail=curr;
298         }
299
300 STACK *ssl_create_cipher_list(ssl_method,cipher_list,cipher_list_by_id,str)
301 SSL_METHOD *ssl_method;
302 STACK **cipher_list,**cipher_list_by_id;
303 char *str;
304         {
305         SSL_CIPHER *c;
306         char *l;
307         STACK *ret=NULL,*ok=NULL;
308 #define CL_BUF  40
309         char buf[CL_BUF];
310         char *tmp_str=NULL;
311         unsigned long mask,algorithms,ma;
312         char *start;
313         int i,j,k,num=0,ch,multi;
314         unsigned long al;
315         STACK *ca_list=NULL;
316         int current_x,num_x;
317         CIPHER_CHOICE *ops=NULL;
318         CIPHER_ORDER *list=NULL,*head=NULL,*tail=NULL,*curr,*tail2,*curr2;
319         int list_num;
320         int type;
321         SSL_CIPHER c_tmp,*cp;
322
323         if (str == NULL) return(NULL);
324
325         if (strncmp(str,"DEFAULT",7) == 0)
326                 {
327                 i=strlen(str)+2+strlen(SSL_DEFAULT_CIPHER_LIST);
328                 if ((tmp_str=Malloc(i)) == NULL)
329                         {
330                         SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
331                         goto err;
332                         }
333                 strcpy(tmp_str,SSL_DEFAULT_CIPHER_LIST);
334                 strcat(tmp_str,":");
335                 strcat(tmp_str,&(str[7]));
336                 str=tmp_str;
337                 }
338         if (init_ciphers) load_ciphers();
339
340         num=ssl_method->num_ciphers();
341
342         if ((ret=(STACK *)sk_new(NULL)) == NULL) goto err;
343         if ((ca_list=(STACK *)sk_new(cmp_by_name)) == NULL) goto err;
344
345         mask =SSL_kFZA;
346 #ifdef NO_RSA
347         mask|=SSL_aRSA|SSL_kRSA;
348 #endif
349 #ifdef NO_DSA
350         mask|=SSL_aDSS;
351 #endif
352 #ifdef NO_DH
353         mask|=SSL_kDHr|SSL_kDHd|SSL_kEDH|SSL_aDH;
354 #endif
355
356 #ifndef SSL_ALLOW_ENULL
357         mask|=SSL_eNULL;
358 #endif
359
360         mask|=(ssl_cipher_methods[SSL_ENC_DES_IDX ] == NULL)?SSL_DES :0;
361         mask|=(ssl_cipher_methods[SSL_ENC_3DES_IDX] == NULL)?SSL_3DES:0;
362         mask|=(ssl_cipher_methods[SSL_ENC_RC4_IDX ] == NULL)?SSL_RC4 :0;
363         mask|=(ssl_cipher_methods[SSL_ENC_RC2_IDX ] == NULL)?SSL_RC2 :0;
364         mask|=(ssl_cipher_methods[SSL_ENC_IDEA_IDX] == NULL)?SSL_IDEA:0;
365         mask|=(ssl_cipher_methods[SSL_ENC_eFZA_IDX] == NULL)?SSL_eFZA:0;
366
367         mask|=(ssl_digest_methods[SSL_MD_MD5_IDX ] == NULL)?SSL_MD5 :0;
368         mask|=(ssl_digest_methods[SSL_MD_SHA1_IDX] == NULL)?SSL_SHA1:0;
369
370         if ((list=(CIPHER_ORDER *)Malloc(sizeof(CIPHER_ORDER)*num)) == NULL)
371                 goto err;
372
373         /* Get the initial list of ciphers */
374         list_num=0;
375         for (i=0; i<num; i++)
376                 {
377                 c=ssl_method->get_cipher((unsigned int)i);
378                 /* drop those that use any of that is not available */
379                 if ((c != NULL) && c->valid && !(c->algorithms & mask))
380                         {
381                         list[list_num].cipher=c;
382                         list[list_num].next=NULL;
383                         list[list_num].prev=NULL;
384                         list[list_num].active=0;
385                         list_num++;
386                         if (!sk_push(ca_list,(char *)c)) goto err;
387                         }
388                 }
389         
390         for (i=1; i<list_num-1; i++)
391                 {
392                 list[i].prev= &(list[i-1]);
393                 list[i].next= &(list[i+1]);
394                 }
395         if (list_num > 0)
396                 {
397                 head= &(list[0]);
398                 head->prev=NULL;
399                 head->next= &(list[1]);
400                 tail= &(list[list_num-1]);
401                 tail->prev= &(list[list_num-2]);
402                 tail->next=NULL;
403                 }
404
405         /* special case */
406         cipher_aliases[0].algorithms= ~mask;
407
408         /* get the aliases */
409         k=sizeof(cipher_aliases)/sizeof(SSL_CIPHER);
410         for (j=0; j<k; j++)
411                 {
412                 al=cipher_aliases[j].algorithms;
413                 /* Drop those that are not relevent */
414                 if ((al & mask) == al) continue;
415                 if (!sk_push(ca_list,(char *)&(cipher_aliases[j]))) goto err;
416                 }
417
418         /* ca_list now holds a 'stack' of SSL_CIPHERS, some real, some
419          * 'aliases' */
420
421         /* how many parameters are there? */
422         num=1;
423         for (l=str; *l; l++)
424                 if (ITEM_SEP(*l))
425                         num++;
426         ops=(CIPHER_CHOICE *)Malloc(sizeof(CIPHER_CHOICE)*num);
427         if (ops == NULL) goto err;
428         memset(ops,0,sizeof(CIPHER_CHOICE)*num);
429
430         /* we now parse the input string and create our operations */
431         l=str;
432         i=0;
433         current_x=0;
434
435         for (;;)
436                 {
437                 ch= *l;
438
439                 if (ch == '\0') break;
440
441                 if (ch == '-')
442                         { j=CIPHER_DEL; l++; }
443                 else if (ch == '+')
444                         { j=CIPHER_ORD; l++; }
445                 else if (ch == '!')
446                         { j=CIPHER_KILL; l++; }
447                 else    
448                         { j=CIPHER_ADD; }
449
450                 if (ITEM_SEP(ch))
451                         {
452                         l++;
453                         continue;
454                         }
455                 ops[current_x].type=j;
456                 ops[current_x].algorithms=0;
457                 ops[current_x].mask=0;
458
459                 start=l;
460                 for (;;)
461                         {
462                         ch= *l;
463                         i=0;
464                         while ( ((ch >= 'A') && (ch <= 'Z')) ||
465                                 ((ch >= '0') && (ch <= '9')) ||
466                                 ((ch >= 'a') && (ch <= 'z')) ||
467                                  (ch == '-'))
468                                  {
469                                  buf[i]=ch;
470                                  ch= *(++l);
471                                  i++;
472                                  if (i >= (CL_BUF-2)) break;
473                                  }
474                         buf[i]='\0';
475
476                         /* check for multi-part specification */
477                         if (ch == '+')
478                                 {
479                                 multi=1;
480                                 l++;
481                                 }
482                         else
483                                 multi=0;
484
485                         c_tmp.name=buf;
486                         j=sk_find(ca_list,(char *)&c_tmp);
487                         if (j < 0)
488                                 goto end_loop;
489
490                         cp=(SSL_CIPHER *)sk_value(ca_list,j);
491                         ops[current_x].algorithms|=cp->algorithms;
492                         /* We add the SSL_SSL_MASK so we can match the
493                          * SSLv2 and SSLv3 versions of RC4-MD5 */
494                         ops[current_x].mask|=cp->mask;
495                         if (!multi) break;
496                         }
497                 current_x++;
498                 if (ch == '\0') break;
499 end_loop:
500                 /* Make sure we scan until the next valid start point */
501                 while ((*l != '\0') && ITEM_SEP(*l))
502                         l++;
503                 }
504
505         num_x=current_x;
506         current_x=0;
507
508         /* We will now process the list of ciphers, once for each category, to
509          * decide what we should do with it. */
510         for (j=0; j<num_x; j++)
511                 {
512                 algorithms=ops[j].algorithms;
513                 type=ops[j].type;
514                 mask=ops[j].mask;
515
516                 curr=head;
517                 curr2=head;
518                 tail2=tail;
519                 for (;;)
520                         {
521                         if ((curr == NULL) || (curr == tail2)) break;
522                         curr=curr2;
523                         curr2=curr->next;
524
525                         cp=curr->cipher;
526                         ma=mask & cp->algorithms;
527                         if ((ma == 0) || ((ma & algorithms) != ma))
528                                 {
529                                 /* does not apply */
530                                 continue;
531                                 }
532
533                         /* add the cipher if it has not been added yet. */
534                         if (type == CIPHER_ADD)
535                                 {
536                                 if (!curr->active)
537                                         {
538                                         ll_append_tail(&head,curr,&tail);
539                                         curr->active=1;
540                                         }
541                                 }
542                         /* Move the added cipher to this location */
543                         else if (type == CIPHER_ORD)
544                                 {
545                                 if (curr->active)
546                                         {
547                                         ll_append_tail(&head,curr,&tail);
548                                         }
549                                 }
550                         else if (type == CIPHER_DEL)
551                                 curr->active=0;
552                         if (type == CIPHER_KILL)
553                                 {
554                                 if (head == curr)
555                                         head=curr->next;
556                                 else
557                                         curr->prev->next=curr->next;
558                                 if (tail == curr)
559                                         tail=curr->prev;
560                                 curr->active=0;
561                                 if (curr->next != NULL)
562                                         curr->next->prev=curr->prev;
563                                 if (curr->prev != NULL)
564                                         curr->prev->next=curr->next;
565                                 curr->next=NULL;
566                                 curr->prev=NULL;
567                                 }
568                         }
569                 }
570
571         for (curr=head; curr != NULL; curr=curr->next)
572                 {
573                 if (curr->active)
574                         {
575                         sk_push(ret,(char *)curr->cipher);
576 #ifdef CIPHER_DEBUG
577                         printf("<%s>\n",curr->cipher->name);
578 #endif
579                         }
580                 }
581
582         if (cipher_list != NULL)
583                 {
584                 if (*cipher_list != NULL)
585                         sk_free(*cipher_list);
586                 *cipher_list=ret;
587                 }
588
589         if (cipher_list_by_id != NULL)
590                 {
591                 if (*cipher_list_by_id != NULL)
592                         sk_free(*cipher_list_by_id);
593                 *cipher_list_by_id=sk_dup(ret);
594                 }
595
596         if (    (cipher_list_by_id == NULL) ||
597                 (*cipher_list_by_id == NULL) ||
598                 (cipher_list == NULL) ||
599                 (*cipher_list == NULL))
600                 goto err;
601         sk_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);
602
603         ok=ret;
604         ret=NULL;
605 err:
606         if (tmp_str) Free(tmp_str);
607         if (ops != NULL) Free(ops);
608         if (ret != NULL) sk_free(ret);
609         if (ca_list != NULL) sk_free(ca_list);
610         if (list != NULL) Free(list);
611         return(ok);
612         }
613
614 char *SSL_CIPHER_description(cipher,buf,len)
615 SSL_CIPHER *cipher;
616 char *buf;
617 int len;
618         {
619         int _export,pkl,kl;
620         char *ver,*exp;
621         char *kx,*au,*enc,*mac;
622         unsigned long alg,alg2;
623         static char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s\n";
624         
625         alg=cipher->algorithms;
626         alg2=cipher->algorithm2;
627
628         _export=SSL_IS_EXPORT(alg);
629         pkl=SSL_EXPORT_PKEYLENGTH(alg);
630         kl=SSL_EXPORT_KEYLENGTH(alg);
631         exp=_export?" export":"";
632
633         if (alg & SSL_SSLV2)
634                 ver="SSLv2";
635         else if (alg & SSL_SSLV3)
636                 ver="SSLv3";
637         else
638                 ver="unknown";
639
640         switch (alg&SSL_MKEY_MASK)
641                 {
642         case SSL_kRSA:
643                 kx=_export?(pkl == 512 ? "RSA(512)" : "RSA(1024)"):"RSA";
644                 break;
645         case SSL_kDHr:
646                 kx="DH/RSA";
647                 break;
648         case SSL_kDHd:
649                 kx="DH/DSS";
650                 break;
651         case SSL_kFZA:
652                 kx="Fortezza";
653                 break;
654         case SSL_kEDH:
655                 kx=_export?(pkl == 512 ? "DH(512)" : "DH(1024)"):"DH";
656                 break;
657         default:
658                 kx="unknown";
659                 }
660
661         switch (alg&SSL_AUTH_MASK)
662                 {
663         case SSL_aRSA:
664                 au="RSA";
665                 break;
666         case SSL_aDSS:
667                 au="DSS";
668                 break;
669         case SSL_aDH:
670                 au="DH";
671                 break;
672         case SSL_aFZA:
673         case SSL_aNULL:
674                 au="None";
675                 break;
676         default:
677                 au="unknown";
678                 break;
679                 }
680
681         switch (alg&SSL_ENC_MASK)
682                 {
683         case SSL_DES:
684                 enc=(_export && kl == 5)?"DES(40)":"DES(56)";
685                 break;
686         case SSL_3DES:
687                 enc="3DES(168)";
688                 break;
689         case SSL_RC4:
690                 enc=_export?(kl == 5 ? "RC4(40)" : "RC4(56)")
691                   :((alg2&SSL2_CF_8_BYTE_ENC)?"RC4(64)":"RC4(128)");
692                 break;
693         case SSL_RC2:
694                 enc=_export?(kl == 5 ? "RC2(40)" : "RC2(56)"):"RC2(128)";
695                 break;
696         case SSL_IDEA:
697                 enc="IDEA(128)";
698                 break;
699         case SSL_eFZA:
700                 enc="Fortezza";
701                 break;
702         case SSL_eNULL:
703                 enc="None";
704                 break;
705         default:
706                 enc="unknown";
707                 break;
708                 }
709
710         switch (alg&SSL_MAC_MASK)
711                 {
712         case SSL_MD5:
713                 mac="MD5";
714                 break;
715         case SSL_SHA1:
716                 mac="SHA1";
717                 break;
718         default:
719                 mac="unknown";
720                 break;
721                 }
722
723         if (buf == NULL)
724                 {
725                 buf=Malloc(128);
726                 if (buf == NULL) return("Malloc Error");
727                 }
728         else if (len < 128)
729                 return("Buffer too small");
730
731         sprintf(buf,format,cipher->name,ver,kx,au,enc,mac,exp);
732         return(buf);
733         }
734
735 char *SSL_CIPHER_get_version(c)
736 SSL_CIPHER *c;
737         {
738         int i;
739
740         if (c == NULL) return("(NONE)");
741         i=(int)(c->id>>24L);
742         if (i == 3)
743                 return("TLSv1/SSLv3");
744         else if (i == 2)
745                 return("SSLv2");
746         else
747                 return("unknown");
748         }
749
750 /* return the actual cipher being used */
751 char *SSL_CIPHER_get_name(c)
752 SSL_CIPHER *c;
753         {
754         if (c != NULL)
755                 return(c->name);
756         return("(NONE)");
757         }
758
759 /* number of bits for symetric cipher */
760 int SSL_CIPHER_get_bits(c,alg_bits)
761 SSL_CIPHER *c;
762 int *alg_bits;
763         {
764         int ret=0,a=0;
765         EVP_CIPHER *enc;
766         EVP_MD *md;
767         SSL_SESSION ss;
768
769         if (c != NULL)
770                 {
771                 ss.cipher=c;
772                 if (!ssl_cipher_get_evp(&ss,&enc,&md,NULL))
773                         return(0);
774
775                 a=EVP_CIPHER_key_length(enc)*8;
776
777                 if (SSL_C_IS_EXPORT(c))
778                         {
779                         ret=SSL_C_EXPORT_KEYLENGTH(c)*8;
780                         }
781                 else
782                         {
783                         if (c->algorithm2 & SSL2_CF_8_BYTE_ENC)
784                                 ret=64;
785                         else
786                                 ret=a;
787                         }
788                 }
789
790         if (alg_bits != NULL) *alg_bits=a;
791         
792         return(ret);
793         }
794
795 SSL_COMP *ssl3_comp_find(sk,n)
796 STACK *sk;
797 int n;
798         {
799         SSL_COMP *ctmp;
800         int i,nn;
801
802         if ((n == 0) || (sk == NULL)) return(NULL);
803         nn=sk_num(sk);
804         for (i=0; i<nn; i++)
805                 {
806                 ctmp=(SSL_COMP *)sk_value(sk,i);
807                 if (ctmp->id == n)
808                         return(ctmp);
809                 }
810         return(NULL);
811         }
812
813 static int sk_comp_cmp(a,b)
814 SSL_COMP **a,**b;
815         {
816         return((*a)->id-(*b)->id);
817         }
818
819 STACK *SSL_COMP_get_compression_methods()
820         {
821         return(ssl_comp_methods);
822         }
823
824 int SSL_COMP_add_compression_method(id,cm)
825 int id;
826 COMP_METHOD *cm;
827         {
828         SSL_COMP *comp;
829         STACK *sk;
830
831         comp=(SSL_COMP *)Malloc(sizeof(SSL_COMP));
832         comp->id=id;
833         comp->method=cm;
834         if (ssl_comp_methods == NULL)
835                 sk=ssl_comp_methods=sk_new(sk_comp_cmp);
836         else
837                 sk=ssl_comp_methods;
838         if ((sk == NULL) || !sk_push(sk,(char *)comp))
839                 {
840                 SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,ERR_R_MALLOC_FAILURE);
841                 return(0);
842                 }
843         else
844                 return(1);
845         }
846