1f884c5a6b031177c080fe9297e27bf339ce1311
[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  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60  * ECC cipher suite support in OpenSSL originally developed by 
61  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
62  */
63 #include <stdio.h>
64 #include <openssl/objects.h>
65 #include <openssl/comp.h>
66 #include "ssl_locl.h"
67
68 #define SSL_ENC_DES_IDX         0
69 #define SSL_ENC_3DES_IDX        1
70 #define SSL_ENC_RC4_IDX         2
71 #define SSL_ENC_RC2_IDX         3
72 #define SSL_ENC_IDEA_IDX        4
73 #define SSL_ENC_eFZA_IDX        5
74 #define SSL_ENC_NULL_IDX        6
75 #define SSL_ENC_AES128_IDX      7
76 #define SSL_ENC_AES256_IDX      8
77 #define SSL_ENC_NUM_IDX         9
78 #define SSL_ENC_CAMELLIA128_IDX 9
79 #define SSL_ENC_CAMELLIA256_IDX 10
80 #undef  SSL_ENC_NUM_IDX
81 #define SSL_ENC_NUM_IDX         11
82
83
84 static const EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX]={
85         NULL,NULL,NULL,NULL,NULL,NULL,
86         };
87
88 #define SSL_COMP_NULL_IDX       0
89 #define SSL_COMP_ZLIB_IDX       1
90 #define SSL_COMP_NUM_IDX        2
91
92 static STACK_OF(SSL_COMP) *ssl_comp_methods=NULL;
93
94 #define SSL_MD_MD5_IDX  0
95 #define SSL_MD_SHA1_IDX 1
96 #define SSL_MD_NUM_IDX  2
97 static const EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX]={
98         NULL,NULL,
99         };
100
101 #define CIPHER_ADD      1
102 #define CIPHER_KILL     2
103 #define CIPHER_DEL      3
104 #define CIPHER_ORD      4
105 #define CIPHER_SPECIAL  5
106
107 typedef struct cipher_order_st
108         {
109         SSL_CIPHER *cipher;
110         int active;
111         int dead;
112         struct cipher_order_st *next,*prev;
113         } CIPHER_ORDER;
114
115 static const SSL_CIPHER cipher_aliases[]={
116         /* Don't include eNULL unless specifically enabled. */
117         /* Don't include ECC in ALL because these ciphers are not yet official. */
118         {0,SSL_TXT_ALL, 0,SSL_ALL & ~SSL_eNULL & ~SSL_kECDH & ~SSL_kECDHE, SSL_ALL ,0,0,0,SSL_ALL,SSL_ALL}, /* must be first */
119         /* TODO: COMPLEMENT OF ALL and COMPLEMENT OF DEFAULT do not have ECC cipher suites handled properly. */
120         {0,SSL_TXT_CMPALL,0,SSL_eNULL,0,0,0,0,SSL_ENC_MASK,0},  /* COMPLEMENT OF ALL */
121         {0,SSL_TXT_CMPDEF,0,SSL_ADH, 0,0,0,0,SSL_AUTH_MASK,0},
122         {0,SSL_TXT_kKRB5,0,SSL_kKRB5,0,0,0,0,SSL_MKEY_MASK,0},  /* VRS Kerberos5 */
123         {0,SSL_TXT_kRSA,0,SSL_kRSA,  0,0,0,0,SSL_MKEY_MASK,0},
124         {0,SSL_TXT_kDHr,0,SSL_kDHr,  0,0,0,0,SSL_MKEY_MASK,0},
125         {0,SSL_TXT_kDHd,0,SSL_kDHd,  0,0,0,0,SSL_MKEY_MASK,0},
126         {0,SSL_TXT_kEDH,0,SSL_kEDH,  0,0,0,0,SSL_MKEY_MASK,0},
127         {0,SSL_TXT_kFZA,0,SSL_kFZA,  0,0,0,0,SSL_MKEY_MASK,0},
128         {0,SSL_TXT_DH,  0,SSL_DH,    0,0,0,0,SSL_MKEY_MASK,0},
129         {0,SSL_TXT_ECC, 0,(SSL_kECDH|SSL_kECDHE), 0,0,0,0,SSL_MKEY_MASK,0},
130         {0,SSL_TXT_EDH, 0,SSL_EDH,   0,0,0,0,SSL_MKEY_MASK|SSL_AUTH_MASK,0},
131         {0,SSL_TXT_aKRB5,0,SSL_aKRB5,0,0,0,0,SSL_AUTH_MASK,0},  /* VRS Kerberos5 */
132         {0,SSL_TXT_aRSA,0,SSL_aRSA,  0,0,0,0,SSL_AUTH_MASK,0},
133         {0,SSL_TXT_aDSS,0,SSL_aDSS,  0,0,0,0,SSL_AUTH_MASK,0},
134         {0,SSL_TXT_aFZA,0,SSL_aFZA,  0,0,0,0,SSL_AUTH_MASK,0},
135         {0,SSL_TXT_aNULL,0,SSL_aNULL,0,0,0,0,SSL_AUTH_MASK,0},
136         {0,SSL_TXT_aDH, 0,SSL_aDH,   0,0,0,0,SSL_AUTH_MASK,0},
137         {0,SSL_TXT_DSS, 0,SSL_DSS,   0,0,0,0,SSL_AUTH_MASK,0},
138
139         {0,SSL_TXT_DES, 0,SSL_DES,   0,0,0,0,SSL_ENC_MASK,0},
140         {0,SSL_TXT_3DES,0,SSL_3DES,  0,0,0,0,SSL_ENC_MASK,0},
141         {0,SSL_TXT_RC4, 0,SSL_RC4,   0,0,0,0,SSL_ENC_MASK,0},
142         {0,SSL_TXT_RC2, 0,SSL_RC2,   0,0,0,0,SSL_ENC_MASK,0},
143 #ifndef OPENSSL_NO_IDEA
144         {0,SSL_TXT_IDEA,0,SSL_IDEA,  0,0,0,0,SSL_ENC_MASK,0},
145 #endif
146         {0,SSL_TXT_eNULL,0,SSL_eNULL,0,0,0,0,SSL_ENC_MASK,0},
147         {0,SSL_TXT_eFZA,0,SSL_eFZA,  0,0,0,0,SSL_ENC_MASK,0},
148         {0,SSL_TXT_AES, 0,SSL_AES,   0,0,0,0,SSL_ENC_MASK,0},
149         {0,SSL_TXT_CAMELLIA,    0,SSL_CAMELLIA,   0,0,0,0,SSL_ENC_MASK,0},
150
151         {0,SSL_TXT_MD5, 0,SSL_MD5,   0,0,0,0,SSL_MAC_MASK,0},
152         {0,SSL_TXT_SHA1,0,SSL_SHA1,  0,0,0,0,SSL_MAC_MASK,0},
153         {0,SSL_TXT_SHA, 0,SSL_SHA,   0,0,0,0,SSL_MAC_MASK,0},
154
155         {0,SSL_TXT_NULL,0,SSL_NULL,  0,0,0,0,SSL_ENC_MASK,0},
156         {0,SSL_TXT_KRB5,0,SSL_KRB5,  0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK,0},
157         {0,SSL_TXT_RSA, 0,SSL_RSA,   0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK,0},
158         {0,SSL_TXT_ADH, 0,SSL_ADH,   0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK,0},
159         {0,SSL_TXT_FZA, 0,SSL_FZA,   0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK|SSL_ENC_MASK,0},
160
161         {0,SSL_TXT_SSLV2, 0,SSL_SSLV2, 0,0,0,0,SSL_SSL_MASK,0},
162         {0,SSL_TXT_SSLV3, 0,SSL_SSLV3, 0,0,0,0,SSL_SSL_MASK,0},
163         {0,SSL_TXT_TLSV1, 0,SSL_TLSV1, 0,0,0,0,SSL_SSL_MASK,0},
164
165         {0,SSL_TXT_EXP   ,0, 0,SSL_EXPORT, 0,0,0,0,SSL_EXP_MASK},
166         {0,SSL_TXT_EXPORT,0, 0,SSL_EXPORT, 0,0,0,0,SSL_EXP_MASK},
167         {0,SSL_TXT_EXP40, 0, 0, SSL_EXP40, 0,0,0,0,SSL_STRONG_MASK},
168         {0,SSL_TXT_EXP56, 0, 0, SSL_EXP56, 0,0,0,0,SSL_STRONG_MASK},
169         {0,SSL_TXT_LOW,   0, 0,   SSL_LOW, 0,0,0,0,SSL_STRONG_MASK},
170         {0,SSL_TXT_MEDIUM,0, 0,SSL_MEDIUM, 0,0,0,0,SSL_STRONG_MASK},
171         {0,SSL_TXT_HIGH,  0, 0,  SSL_HIGH, 0,0,0,0,SSL_STRONG_MASK},
172         };
173
174 void ssl_load_ciphers(void)
175         {
176         ssl_cipher_methods[SSL_ENC_DES_IDX]= 
177                 EVP_get_cipherbyname(SN_des_cbc);
178         ssl_cipher_methods[SSL_ENC_3DES_IDX]=
179                 EVP_get_cipherbyname(SN_des_ede3_cbc);
180         ssl_cipher_methods[SSL_ENC_RC4_IDX]=
181                 EVP_get_cipherbyname(SN_rc4);
182         ssl_cipher_methods[SSL_ENC_RC2_IDX]= 
183                 EVP_get_cipherbyname(SN_rc2_cbc);
184 #ifndef OPENSSL_NO_IDEA
185         ssl_cipher_methods[SSL_ENC_IDEA_IDX]= 
186                 EVP_get_cipherbyname(SN_idea_cbc);
187 #else
188         ssl_cipher_methods[SSL_ENC_IDEA_IDX]= NULL;
189 #endif
190         ssl_cipher_methods[SSL_ENC_AES128_IDX]=
191           EVP_get_cipherbyname(SN_aes_128_cbc);
192         ssl_cipher_methods[SSL_ENC_AES256_IDX]=
193           EVP_get_cipherbyname(SN_aes_256_cbc);
194         ssl_cipher_methods[SSL_ENC_CAMELLIA128_IDX]=
195           EVP_get_cipherbyname(SN_camellia_128_cbc);
196         ssl_cipher_methods[SSL_ENC_CAMELLIA256_IDX]=
197           EVP_get_cipherbyname(SN_camellia_256_cbc);
198
199         ssl_digest_methods[SSL_MD_MD5_IDX]=
200                 EVP_get_digestbyname(SN_md5);
201         ssl_digest_methods[SSL_MD_SHA1_IDX]=
202                 EVP_get_digestbyname(SN_sha1);
203         }
204
205
206 #ifndef OPENSSL_NO_COMP
207
208 static int sk_comp_cmp(const SSL_COMP * const *a,
209                         const SSL_COMP * const *b)
210         {
211         return((*a)->id-(*b)->id);
212         }
213
214 static void load_builtin_compressions(void)
215         {
216         if (ssl_comp_methods != NULL)
217                 return;
218
219         CRYPTO_w_lock(CRYPTO_LOCK_SSL);
220         if (ssl_comp_methods == NULL)
221                 {
222                 SSL_COMP *comp = NULL;
223
224                 MemCheck_off();
225                 ssl_comp_methods=sk_SSL_COMP_new(sk_comp_cmp);
226                 if (ssl_comp_methods != NULL)
227                         {
228                         comp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP));
229                         if (comp != NULL)
230                                 {
231                                 comp->method=COMP_zlib();
232                                 if (comp->method
233                                         && comp->method->type == NID_undef)
234                                         OPENSSL_free(comp);
235                                 else
236                                         {
237                                         comp->id=SSL_COMP_ZLIB_IDX;
238                                         comp->name=comp->method->name;
239                                         sk_SSL_COMP_push(ssl_comp_methods,comp);
240                                         }
241                                 }
242                         }
243                 MemCheck_on();
244                 }
245         CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
246         }
247 #endif
248
249 int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
250              const EVP_MD **md, SSL_COMP **comp)
251         {
252         int i;
253         SSL_CIPHER *c;
254
255         c=s->cipher;
256         if (c == NULL) return(0);
257         if (comp != NULL)
258                 {
259                 SSL_COMP ctmp;
260 #ifndef OPENSSL_NO_COMP
261                 load_builtin_compressions();
262 #endif
263
264                 *comp=NULL;
265                 ctmp.id=s->compress_meth;
266                 if (ssl_comp_methods != NULL)
267                         {
268                         i=sk_SSL_COMP_find(ssl_comp_methods,&ctmp);
269                         if (i >= 0)
270                                 *comp=sk_SSL_COMP_value(ssl_comp_methods,i);
271                         else
272                                 *comp=NULL;
273                         }
274                 }
275
276         if ((enc == NULL) || (md == NULL)) return(0);
277
278         switch (c->algorithms & SSL_ENC_MASK)
279                 {
280         case SSL_DES:
281                 i=SSL_ENC_DES_IDX;
282                 break;
283         case SSL_3DES:
284                 i=SSL_ENC_3DES_IDX;
285                 break;
286         case SSL_RC4:
287                 i=SSL_ENC_RC4_IDX;
288                 break;
289         case SSL_RC2:
290                 i=SSL_ENC_RC2_IDX;
291                 break;
292         case SSL_IDEA:
293                 i=SSL_ENC_IDEA_IDX;
294                 break;
295         case SSL_eNULL:
296                 i=SSL_ENC_NULL_IDX;
297                 break;
298         case SSL_AES:
299                 switch(c->alg_bits)
300                         {
301                 case 128: i=SSL_ENC_AES128_IDX; break;
302                 case 256: i=SSL_ENC_AES256_IDX; break;
303                 default: i=-1; break;
304                         }
305                 break;
306         case SSL_CAMELLIA:
307                 switch(c->alg_bits)
308                         {
309                 case 128: i=SSL_ENC_CAMELLIA128_IDX; break;
310                 case 256: i=SSL_ENC_CAMELLIA256_IDX; break;
311                 default: i=-1; break;
312                         }
313                 break;
314
315         default:
316                 i= -1;
317                 break;
318                 }
319
320         if ((i < 0) || (i > SSL_ENC_NUM_IDX))
321                 *enc=NULL;
322         else
323                 {
324                 if (i == SSL_ENC_NULL_IDX)
325                         *enc=EVP_enc_null();
326                 else
327                         *enc=ssl_cipher_methods[i];
328                 }
329
330         switch (c->algorithms & SSL_MAC_MASK)
331                 {
332         case SSL_MD5:
333                 i=SSL_MD_MD5_IDX;
334                 break;
335         case SSL_SHA1:
336                 i=SSL_MD_SHA1_IDX;
337                 break;
338         default:
339                 i= -1;
340                 break;
341                 }
342         if ((i < 0) || (i > SSL_MD_NUM_IDX))
343                 *md=NULL;
344         else
345                 *md=ssl_digest_methods[i];
346
347         if ((*enc != NULL) && (*md != NULL))
348                 return(1);
349         else
350                 return(0);
351         }
352
353 #define ITEM_SEP(a) \
354         (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
355
356 static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
357              CIPHER_ORDER **tail)
358         {
359         if (curr == *tail) return;
360         if (curr == *head)
361                 *head=curr->next;
362         if (curr->prev != NULL)
363                 curr->prev->next=curr->next;
364         if (curr->next != NULL) /* should always be true */
365                 curr->next->prev=curr->prev;
366         (*tail)->next=curr;
367         curr->prev= *tail;
368         curr->next=NULL;
369         *tail=curr;
370         }
371
372 static unsigned long ssl_cipher_get_disabled(void)
373         {
374         unsigned long mask;
375
376         mask = SSL_kFZA;
377 #ifdef OPENSSL_NO_RSA
378         mask |= SSL_aRSA|SSL_kRSA;
379 #endif
380 #ifdef OPENSSL_NO_DSA
381         mask |= SSL_aDSS;
382 #endif
383 #ifdef OPENSSL_NO_DH
384         mask |= SSL_kDHr|SSL_kDHd|SSL_kEDH|SSL_aDH;
385 #endif
386 #ifdef OPENSSL_NO_KRB5
387         mask |= SSL_kKRB5|SSL_aKRB5;
388 #endif
389 #ifdef OPENSSL_NO_ECDH
390         mask |= SSL_kECDH|SSL_kECDHE;
391 #endif
392 #ifdef SSL_FORBID_ENULL
393         mask |= SSL_eNULL;
394 #endif
395
396         mask |= (ssl_cipher_methods[SSL_ENC_DES_IDX ] == NULL) ? SSL_DES :0;
397         mask |= (ssl_cipher_methods[SSL_ENC_3DES_IDX] == NULL) ? SSL_3DES:0;
398         mask |= (ssl_cipher_methods[SSL_ENC_RC4_IDX ] == NULL) ? SSL_RC4 :0;
399         mask |= (ssl_cipher_methods[SSL_ENC_RC2_IDX ] == NULL) ? SSL_RC2 :0;
400         mask |= (ssl_cipher_methods[SSL_ENC_IDEA_IDX] == NULL) ? SSL_IDEA:0;
401         mask |= (ssl_cipher_methods[SSL_ENC_eFZA_IDX] == NULL) ? SSL_eFZA:0;
402         mask |= (ssl_cipher_methods[SSL_ENC_AES128_IDX] == NULL) ? SSL_AES:0;
403         mask |= (ssl_cipher_methods[SSL_ENC_CAMELLIA128_IDX] == NULL) ? SSL_CAMELLIA:0;
404
405         mask |= (ssl_digest_methods[SSL_MD_MD5_IDX ] == NULL) ? SSL_MD5 :0;
406         mask |= (ssl_digest_methods[SSL_MD_SHA1_IDX] == NULL) ? SSL_SHA1:0;
407
408         return(mask);
409         }
410
411 static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
412                 int num_of_ciphers, unsigned long mask, CIPHER_ORDER *co_list,
413                 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
414         {
415         int i, co_list_num;
416         SSL_CIPHER *c;
417
418         /*
419          * We have num_of_ciphers descriptions compiled in, depending on the
420          * method selected (SSLv2 and/or SSLv3, TLSv1 etc).
421          * These will later be sorted in a linked list with at most num
422          * entries.
423          */
424
425         /* Get the initial list of ciphers */
426         co_list_num = 0;        /* actual count of ciphers */
427         for (i = 0; i < num_of_ciphers; i++)
428                 {
429                 c = ssl_method->get_cipher(i);
430                 /* drop those that use any of that is not available */
431                 if ((c != NULL) && c->valid && !(c->algorithms & mask))
432                         {
433                         co_list[co_list_num].cipher = c;
434                         co_list[co_list_num].next = NULL;
435                         co_list[co_list_num].prev = NULL;
436                         co_list[co_list_num].active = 0;
437                         co_list_num++;
438 #ifdef KSSL_DEBUG
439                         printf("\t%d: %s %lx %lx\n",i,c->name,c->id,c->algorithms);
440 #endif  /* KSSL_DEBUG */
441                         /*
442                         if (!sk_push(ca_list,(char *)c)) goto err;
443                         */
444                         }
445                 }
446
447         /*
448          * Prepare linked list from list entries
449          */     
450         for (i = 1; i < co_list_num - 1; i++)
451                 {
452                 co_list[i].prev = &(co_list[i-1]);
453                 co_list[i].next = &(co_list[i+1]);
454                 }
455         if (co_list_num > 0)
456                 {
457                 (*head_p) = &(co_list[0]);
458                 (*head_p)->prev = NULL;
459                 (*head_p)->next = &(co_list[1]);
460                 (*tail_p) = &(co_list[co_list_num - 1]);
461                 (*tail_p)->prev = &(co_list[co_list_num - 2]);
462                 (*tail_p)->next = NULL;
463                 }
464         }
465
466 static void ssl_cipher_collect_aliases(SSL_CIPHER **ca_list,
467                         int num_of_group_aliases, unsigned long mask,
468                         CIPHER_ORDER *head)
469         {
470         CIPHER_ORDER *ciph_curr;
471         SSL_CIPHER **ca_curr;
472         int i;
473
474         /*
475          * First, add the real ciphers as already collected
476          */
477         ciph_curr = head;
478         ca_curr = ca_list;
479         while (ciph_curr != NULL)
480                 {
481                 *ca_curr = ciph_curr->cipher;
482                 ca_curr++;
483                 ciph_curr = ciph_curr->next;
484                 }
485
486         /*
487          * Now we add the available ones from the cipher_aliases[] table.
488          * They represent either an algorithm, that must be fully
489          * supported (not match any bit in mask) or represent a cipher
490          * strength value (will be added in any case because algorithms=0).
491          */
492         for (i = 0; i < num_of_group_aliases; i++)
493                 {
494                 if ((i == 0) ||         /* always fetch "ALL" */
495                     !(cipher_aliases[i].algorithms & mask))
496                         {
497                         *ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
498                         ca_curr++;
499                         }
500                 }
501
502         *ca_curr = NULL;        /* end of list */
503         }
504
505 static void ssl_cipher_apply_rule(unsigned long cipher_id,
506                 unsigned long algorithms, unsigned long mask,
507                 unsigned long algo_strength, unsigned long mask_strength,
508                 int rule, int strength_bits, CIPHER_ORDER *co_list,
509                 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
510         {
511         CIPHER_ORDER *head, *tail, *curr, *curr2, *tail2;
512         SSL_CIPHER *cp;
513         unsigned long ma, ma_s;
514
515 #ifdef CIPHER_DEBUG
516         printf("Applying rule %d with %08lx %08lx %08lx %08lx (%d)\n",
517                 rule, algorithms, mask, algo_strength, mask_strength,
518                 strength_bits);
519 #endif
520
521         curr = head = *head_p;
522         curr2 = head;
523         tail2 = tail = *tail_p;
524         for (;;)
525                 {
526                 if ((curr == NULL) || (curr == tail2)) break;
527                 curr = curr2;
528                 curr2 = curr->next;
529
530                 cp = curr->cipher;
531
532                 /* If explicit cipher suite match that one only */
533
534                 if (cipher_id)
535                         {
536                         if (cp->id != cipher_id)
537                                 continue;
538                         }
539
540                 /*
541                  * Selection criteria is either the number of strength_bits
542                  * or the algorithm used.
543                  */
544                 else if (strength_bits == -1)
545                         {
546                         ma = mask & cp->algorithms;
547                         ma_s = mask_strength & cp->algo_strength;
548
549 #ifdef CIPHER_DEBUG
550                         printf("\nName: %s:\nAlgo = %08lx Algo_strength = %08lx\nMask = %08lx Mask_strength %08lx\n", cp->name, cp->algorithms, cp->algo_strength, mask, mask_strength);
551                         printf("ma = %08lx ma_s %08lx, ma&algo=%08lx, ma_s&algos=%08lx\n", ma, ma_s, ma&algorithms, ma_s&algo_strength);
552 #endif
553                         /*
554                          * Select: if none of the mask bit was met from the
555                          * cipher or not all of the bits were met, the
556                          * selection does not apply.
557                          */
558                         if (((ma == 0) && (ma_s == 0)) ||
559                             ((ma & algorithms) != ma) ||
560                             ((ma_s & algo_strength) != ma_s))
561                                 continue; /* does not apply */
562                         }
563                 else if (strength_bits != cp->strength_bits)
564                         continue;       /* does not apply */
565
566 #ifdef CIPHER_DEBUG
567                 printf("Action = %d\n", rule);
568 #endif
569
570                 /* add the cipher if it has not been added yet. */
571                 if (rule == CIPHER_ADD)
572                         {
573                         if (!curr->active)
574                                 {
575                                 ll_append_tail(&head, curr, &tail);
576                                 curr->active = 1;
577                                 }
578                         }
579                 /* Move the added cipher to this location */
580                 else if (rule == CIPHER_ORD)
581                         {
582                         if (curr->active)
583                                 {
584                                 ll_append_tail(&head, curr, &tail);
585                                 }
586                         }
587                 else if (rule == CIPHER_DEL)
588                         curr->active = 0;
589                 else if (rule == CIPHER_KILL)
590                         {
591                         if (head == curr)
592                                 head = curr->next;
593                         else
594                                 curr->prev->next = curr->next;
595                         if (tail == curr)
596                                 tail = curr->prev;
597                         curr->active = 0;
598                         if (curr->next != NULL)
599                                 curr->next->prev = curr->prev;
600                         if (curr->prev != NULL)
601                                 curr->prev->next = curr->next;
602                         curr->next = NULL;
603                         curr->prev = NULL;
604                         }
605                 }
606
607         *head_p = head;
608         *tail_p = tail;
609         }
610
611 static int ssl_cipher_strength_sort(CIPHER_ORDER *co_list,
612                                     CIPHER_ORDER **head_p,
613                                     CIPHER_ORDER **tail_p)
614         {
615         int max_strength_bits, i, *number_uses;
616         CIPHER_ORDER *curr;
617
618         /*
619          * This routine sorts the ciphers with descending strength. The sorting
620          * must keep the pre-sorted sequence, so we apply the normal sorting
621          * routine as '+' movement to the end of the list.
622          */
623         max_strength_bits = 0;
624         curr = *head_p;
625         while (curr != NULL)
626                 {
627                 if (curr->active &&
628                     (curr->cipher->strength_bits > max_strength_bits))
629                     max_strength_bits = curr->cipher->strength_bits;
630                 curr = curr->next;
631                 }
632
633         number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
634         if (!number_uses)
635         {
636                 SSLerr(SSL_F_SSL_CIPHER_STRENGTH_SORT,ERR_R_MALLOC_FAILURE);
637                 return(0);
638         }
639         memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
640
641         /*
642          * Now find the strength_bits values actually used
643          */
644         curr = *head_p;
645         while (curr != NULL)
646                 {
647                 if (curr->active)
648                         number_uses[curr->cipher->strength_bits]++;
649                 curr = curr->next;
650                 }
651         /*
652          * Go through the list of used strength_bits values in descending
653          * order.
654          */
655         for (i = max_strength_bits; i >= 0; i--)
656                 if (number_uses[i] > 0)
657                         ssl_cipher_apply_rule(0, 0, 0, 0, 0, CIPHER_ORD, i,
658                                         co_list, head_p, tail_p);
659
660         OPENSSL_free(number_uses);
661         return(1);
662         }
663
664 static int ssl_cipher_process_rulestr(const char *rule_str,
665                 CIPHER_ORDER *co_list, CIPHER_ORDER **head_p,
666                 CIPHER_ORDER **tail_p, SSL_CIPHER **ca_list)
667         {
668         unsigned long algorithms, mask, algo_strength, mask_strength;
669         const char *l, *start, *buf;
670         int j, multi, found, rule, retval, ok, buflen;
671         unsigned long cipher_id;
672         char ch;
673
674         retval = 1;
675         l = rule_str;
676         for (;;)
677                 {
678                 ch = *l;
679
680                 if (ch == '\0')
681                         break;          /* done */
682                 if (ch == '-')
683                         { rule = CIPHER_DEL; l++; }
684                 else if (ch == '+')
685                         { rule = CIPHER_ORD; l++; }
686                 else if (ch == '!')
687                         { rule = CIPHER_KILL; l++; }
688                 else if (ch == '@')
689                         { rule = CIPHER_SPECIAL; l++; }
690                 else
691                         { rule = CIPHER_ADD; }
692
693                 if (ITEM_SEP(ch))
694                         {
695                         l++;
696                         continue;
697                         }
698
699                 algorithms = mask = algo_strength = mask_strength = 0;
700
701                 start=l;
702                 for (;;)
703                         {
704                         ch = *l;
705                         buf = l;
706                         buflen = 0;
707 #ifndef CHARSET_EBCDIC
708                         while ( ((ch >= 'A') && (ch <= 'Z')) ||
709                                 ((ch >= '0') && (ch <= '9')) ||
710                                 ((ch >= 'a') && (ch <= 'z')) ||
711                                  (ch == '-'))
712 #else
713                         while ( isalnum(ch) || (ch == '-'))
714 #endif
715                                  {
716                                  ch = *(++l);
717                                  buflen++;
718                                  }
719
720                         if (buflen == 0)
721                                 {
722                                 /*
723                                  * We hit something we cannot deal with,
724                                  * it is no command or separator nor
725                                  * alphanumeric, so we call this an error.
726                                  */
727                                 SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
728                                        SSL_R_INVALID_COMMAND);
729                                 retval = found = 0;
730                                 l++;
731                                 break;
732                                 }
733
734                         if (rule == CIPHER_SPECIAL)
735                                 {
736                                 found = 0; /* unused -- avoid compiler warning */
737                                 break;  /* special treatment */
738                                 }
739
740                         /* check for multi-part specification */
741                         if (ch == '+')
742                                 {
743                                 multi=1;
744                                 l++;
745                                 }
746                         else
747                                 multi=0;
748
749                         /*
750                          * Now search for the cipher alias in the ca_list. Be careful
751                          * with the strncmp, because the "buflen" limitation
752                          * will make the rule "ADH:SOME" and the cipher
753                          * "ADH-MY-CIPHER" look like a match for buflen=3.
754                          * So additionally check whether the cipher name found
755                          * has the correct length. We can save a strlen() call:
756                          * just checking for the '\0' at the right place is
757                          * sufficient, we have to strncmp() anyway. (We cannot
758                          * use strcmp(), because buf is not '\0' terminated.)
759                          */
760                          j = found = 0;
761                          cipher_id = 0;
762                          while (ca_list[j])
763                                 {
764                                 if (!strncmp(buf, ca_list[j]->name, buflen) &&
765                                     (ca_list[j]->name[buflen] == '\0'))
766                                         {
767                                         found = 1;
768                                         break;
769                                         }
770                                 else
771                                         j++;
772                                 }
773                         if (!found)
774                                 break;  /* ignore this entry */
775
776                         if (ca_list[j]->valid)
777                                 {
778                                 cipher_id = ca_list[j]->id;
779                                 break;
780                                 }
781
782                         /* New algorithms:
783                          *  1 - any old restrictions apply outside new mask
784                          *  2 - any new restrictions apply outside old mask
785                          *  3 - enforce old & new where masks intersect
786                          */
787                         algorithms = (algorithms & ~ca_list[j]->mask) |         /* 1 */
788                                      (ca_list[j]->algorithms & ~mask) |         /* 2 */
789                                      (algorithms & ca_list[j]->algorithms);     /* 3 */
790                         mask |= ca_list[j]->mask;
791                         algo_strength = (algo_strength & ~ca_list[j]->mask_strength) |
792                                         (ca_list[j]->algo_strength & ~mask_strength) |
793                                         (algo_strength & ca_list[j]->algo_strength);
794                         mask_strength |= ca_list[j]->mask_strength;
795
796                         if (!multi) break;
797                         }
798
799                 /*
800                  * Ok, we have the rule, now apply it
801                  */
802                 if (rule == CIPHER_SPECIAL)
803                         {       /* special command */
804                         ok = 0;
805                         if ((buflen == 8) &&
806                                 !strncmp(buf, "STRENGTH", 8))
807                                 ok = ssl_cipher_strength_sort(co_list,
808                                         head_p, tail_p);
809                         else
810                                 SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
811                                         SSL_R_INVALID_COMMAND);
812                         if (ok == 0)
813                                 retval = 0;
814                         /*
815                          * We do not support any "multi" options
816                          * together with "@", so throw away the
817                          * rest of the command, if any left, until
818                          * end or ':' is found.
819                          */
820                         while ((*l != '\0') && ITEM_SEP(*l))
821                                 l++;
822                         }
823                 else if (found)
824                         {
825                         ssl_cipher_apply_rule(cipher_id, algorithms, mask,
826                                 algo_strength, mask_strength, rule, -1,
827                                 co_list, head_p, tail_p);
828                         }
829                 else
830                         {
831                         while ((*l != '\0') && ITEM_SEP(*l))
832                                 l++;
833                         }
834                 if (*l == '\0') break; /* done */
835                 }
836
837         return(retval);
838         }
839
840 STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
841                 STACK_OF(SSL_CIPHER) **cipher_list,
842                 STACK_OF(SSL_CIPHER) **cipher_list_by_id,
843                 const char *rule_str)
844         {
845         int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
846         unsigned long disabled_mask;
847         STACK_OF(SSL_CIPHER) *cipherstack, *tmp_cipher_list;
848         const char *rule_p;
849         CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
850         SSL_CIPHER **ca_list = NULL;
851
852         /*
853          * Return with error if nothing to do.
854          */
855         if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL)
856                 return NULL;
857
858         /*
859          * To reduce the work to do we only want to process the compiled
860          * in algorithms, so we first get the mask of disabled ciphers.
861          */
862         disabled_mask = ssl_cipher_get_disabled();
863
864         /*
865          * Now we have to collect the available ciphers from the compiled
866          * in ciphers. We cannot get more than the number compiled in, so
867          * it is used for allocation.
868          */
869         num_of_ciphers = ssl_method->num_ciphers();
870 #ifdef KSSL_DEBUG
871         printf("ssl_create_cipher_list() for %d ciphers\n", num_of_ciphers);
872 #endif    /* KSSL_DEBUG */
873         co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
874         if (co_list == NULL)
875                 {
876                 SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
877                 return(NULL);   /* Failure */
878                 }
879
880         ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, disabled_mask,
881                                    co_list, &head, &tail);
882
883         /*
884          * We also need cipher aliases for selecting based on the rule_str.
885          * There might be two types of entries in the rule_str: 1) names
886          * of ciphers themselves 2) aliases for groups of ciphers.
887          * For 1) we need the available ciphers and for 2) the cipher
888          * groups of cipher_aliases added together in one list (otherwise
889          * we would be happy with just the cipher_aliases table).
890          */
891         num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
892         num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
893         ca_list =
894                 (SSL_CIPHER **)OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
895         if (ca_list == NULL)
896                 {
897                 OPENSSL_free(co_list);
898                 SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
899                 return(NULL);   /* Failure */
900                 }
901         ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, disabled_mask,
902                                    head);
903
904         /*
905          * If the rule_string begins with DEFAULT, apply the default rule
906          * before using the (possibly available) additional rules.
907          */
908         ok = 1;
909         rule_p = rule_str;
910         if (strncmp(rule_str,"DEFAULT",7) == 0)
911                 {
912                 ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
913                         co_list, &head, &tail, ca_list);
914                 rule_p += 7;
915                 if (*rule_p == ':')
916                         rule_p++;
917                 }
918
919         if (ok && (strlen(rule_p) > 0))
920                 ok = ssl_cipher_process_rulestr(rule_p, co_list, &head, &tail,
921                                                 ca_list);
922
923         OPENSSL_free(ca_list);  /* Not needed anymore */
924
925         if (!ok)
926                 {       /* Rule processing failure */
927                 OPENSSL_free(co_list);
928                 return(NULL);
929                 }
930         /*
931          * Allocate new "cipherstack" for the result, return with error
932          * if we cannot get one.
933          */
934         if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL)
935                 {
936                 OPENSSL_free(co_list);
937                 return(NULL);
938                 }
939
940         /*
941          * The cipher selection for the list is done. The ciphers are added
942          * to the resulting precedence to the STACK_OF(SSL_CIPHER).
943          */
944         for (curr = head; curr != NULL; curr = curr->next)
945                 {
946                 if (curr->active)
947                         {
948                         sk_SSL_CIPHER_push(cipherstack, curr->cipher);
949 #ifdef CIPHER_DEBUG
950                         printf("<%s>\n",curr->cipher->name);
951 #endif
952                         }
953                 }
954         OPENSSL_free(co_list);  /* Not needed any longer */
955
956         tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
957         if (tmp_cipher_list == NULL)
958                 {
959                 sk_SSL_CIPHER_free(cipherstack);
960                 return NULL;
961                 }
962         if (*cipher_list != NULL)
963                 sk_SSL_CIPHER_free(*cipher_list);
964         *cipher_list = cipherstack;
965         if (*cipher_list_by_id != NULL)
966                 sk_SSL_CIPHER_free(*cipher_list_by_id);
967         *cipher_list_by_id = tmp_cipher_list;
968         sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);
969
970         return(cipherstack);
971         }
972
973 char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len)
974         {
975         int is_export,pkl,kl;
976         const char *ver,*exp_str;
977         const char *kx,*au,*enc,*mac;
978         unsigned long alg,alg2,alg_s;
979 #ifdef KSSL_DEBUG
980         static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s AL=%lx\n";
981 #else
982         static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s\n";
983 #endif /* KSSL_DEBUG */
984
985         alg=cipher->algorithms;
986         alg_s=cipher->algo_strength;
987         alg2=cipher->algorithm2;
988
989         is_export=SSL_C_IS_EXPORT(cipher);
990         pkl=SSL_C_EXPORT_PKEYLENGTH(cipher);
991         kl=SSL_C_EXPORT_KEYLENGTH(cipher);
992         exp_str=is_export?" export":"";
993         
994         if (alg & SSL_SSLV2)
995                 ver="SSLv2";
996         else if (alg & SSL_SSLV3)
997                 ver="SSLv3";
998         else
999                 ver="unknown";
1000
1001         switch (alg&SSL_MKEY_MASK)
1002                 {
1003         case SSL_kRSA:
1004                 kx=is_export?(pkl == 512 ? "RSA(512)" : "RSA(1024)"):"RSA";
1005                 break;
1006         case SSL_kDHr:
1007                 kx="DH/RSA";
1008                 break;
1009         case SSL_kDHd:
1010                 kx="DH/DSS";
1011                 break;
1012         case SSL_kKRB5:         /* VRS */
1013         case SSL_KRB5:          /* VRS */
1014             kx="KRB5";
1015             break;
1016         case SSL_kFZA:
1017                 kx="Fortezza";
1018                 break;
1019         case SSL_kEDH:
1020                 kx=is_export?(pkl == 512 ? "DH(512)" : "DH(1024)"):"DH";
1021                 break;
1022         case SSL_kECDH:
1023         case SSL_kECDHE:
1024                 kx=is_export?"ECDH(<=163)":"ECDH";
1025                 break;
1026         default:
1027                 kx="unknown";
1028                 }
1029
1030         switch (alg&SSL_AUTH_MASK)
1031                 {
1032         case SSL_aRSA:
1033                 au="RSA";
1034                 break;
1035         case SSL_aDSS:
1036                 au="DSS";
1037                 break;
1038         case SSL_aDH:
1039                 au="DH";
1040                 break;
1041         case SSL_aKRB5:         /* VRS */
1042         case SSL_KRB5:          /* VRS */
1043             au="KRB5";
1044             break;
1045         case SSL_aFZA:
1046         case SSL_aNULL:
1047                 au="None";
1048                 break;
1049         case SSL_aECDSA:
1050                 au="ECDSA";
1051                 break;
1052         default:
1053                 au="unknown";
1054                 break;
1055                 }
1056
1057         switch (alg&SSL_ENC_MASK)
1058                 {
1059         case SSL_DES:
1060                 enc=(is_export && kl == 5)?"DES(40)":"DES(56)";
1061                 break;
1062         case SSL_3DES:
1063                 enc="3DES(168)";
1064                 break;
1065         case SSL_RC4:
1066                 enc=is_export?(kl == 5 ? "RC4(40)" : "RC4(56)")
1067                   :((alg2&SSL2_CF_8_BYTE_ENC)?"RC4(64)":"RC4(128)");
1068                 break;
1069         case SSL_RC2:
1070                 enc=is_export?(kl == 5 ? "RC2(40)" : "RC2(56)"):"RC2(128)";
1071                 break;
1072         case SSL_IDEA:
1073                 enc="IDEA(128)";
1074                 break;
1075         case SSL_eFZA:
1076                 enc="Fortezza";
1077                 break;
1078         case SSL_eNULL:
1079                 enc="None";
1080                 break;
1081         case SSL_AES:
1082                 switch(cipher->strength_bits)
1083                         {
1084                 case 128: enc="AES(128)"; break;
1085                 case 192: enc="AES(192)"; break;
1086                 case 256: enc="AES(256)"; break;
1087                 default: enc="AES(?""?""?)"; break;
1088                         }
1089                 break;
1090         case SSL_CAMELLIA:
1091                 switch(cipher->strength_bits)
1092                         {
1093                 case 128: enc="Camellia(128)"; break;
1094                 case 256: enc="Camellia(256)"; break;
1095                 default: enc="Camellia(?""?""?)"; break;
1096                         }
1097                 break;
1098                 
1099         default:
1100                 enc="unknown";
1101                 break;
1102                 }
1103
1104         switch (alg&SSL_MAC_MASK)
1105                 {
1106         case SSL_MD5:
1107                 mac="MD5";
1108                 break;
1109         case SSL_SHA1:
1110                 mac="SHA1";
1111                 break;
1112         default:
1113                 mac="unknown";
1114                 break;
1115                 }
1116
1117         if (buf == NULL)
1118                 {
1119                 len=128;
1120                 buf=OPENSSL_malloc(len);
1121                 if (buf == NULL) return("OPENSSL_malloc Error");
1122                 }
1123         else if (len < 128)
1124                 return("Buffer too small");
1125
1126 #ifdef KSSL_DEBUG
1127         BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str,alg);
1128 #else
1129         BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str);
1130 #endif /* KSSL_DEBUG */
1131         return(buf);
1132         }
1133
1134 char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
1135         {
1136         int i;
1137
1138         if (c == NULL) return("(NONE)");
1139         i=(int)(c->id>>24L);
1140         if (i == 3)
1141                 return("TLSv1/SSLv3");
1142         else if (i == 2)
1143                 return("SSLv2");
1144         else
1145                 return("unknown");
1146         }
1147
1148 /* return the actual cipher being used */
1149 const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
1150         {
1151         if (c != NULL)
1152                 return(c->name);
1153         return("(NONE)");
1154         }
1155
1156 /* number of bits for symmetric cipher */
1157 int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
1158         {
1159         int ret=0;
1160
1161         if (c != NULL)
1162                 {
1163                 if (alg_bits != NULL) *alg_bits = c->alg_bits;
1164                 ret = c->strength_bits;
1165                 }
1166         return(ret);
1167         }
1168
1169 SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n)
1170         {
1171         SSL_COMP *ctmp;
1172         int i,nn;
1173
1174         if ((n == 0) || (sk == NULL)) return(NULL);
1175         nn=sk_SSL_COMP_num(sk);
1176         for (i=0; i<nn; i++)
1177                 {
1178                 ctmp=sk_SSL_COMP_value(sk,i);
1179                 if (ctmp->id == n)
1180                         return(ctmp);
1181                 }
1182         return(NULL);
1183         }
1184
1185 #ifdef OPENSSL_NO_COMP
1186 void *SSL_COMP_get_compression_methods(void)
1187         {
1188         return NULL;
1189         }
1190 int SSL_COMP_add_compression_method(int id, void *cm)
1191         {
1192         return 1;
1193         }
1194
1195 const char *SSL_COMP_get_name(const void *comp)
1196         {
1197         return NULL;
1198         }
1199 #else
1200 STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
1201         {
1202         load_builtin_compressions();
1203         return(ssl_comp_methods);
1204         }
1205
1206 int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
1207         {
1208         SSL_COMP *comp;
1209
1210         if (cm == NULL || cm->type == NID_undef)
1211                 return 1;
1212
1213         /* According to draft-ietf-tls-compression-04.txt, the
1214            compression number ranges should be the following:
1215
1216            0 to 63:    methods defined by the IETF
1217            64 to 192:  external party methods assigned by IANA
1218            193 to 255: reserved for private use */
1219         if (id < 193 || id > 255)
1220                 {
1221                 SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,SSL_R_COMPRESSION_ID_NOT_WITHIN_PRIVATE_RANGE);
1222                 return 0;
1223                 }
1224
1225         MemCheck_off();
1226         comp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP));
1227         comp->id=id;
1228         comp->method=cm;
1229         load_builtin_compressions();
1230         if (ssl_comp_methods
1231                 && !sk_SSL_COMP_find(ssl_comp_methods,comp))
1232                 {
1233                 OPENSSL_free(comp);
1234                 MemCheck_on();
1235                 SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,SSL_R_DUPLICATE_COMPRESSION_ID);
1236                 return(1);
1237                 }
1238         else if ((ssl_comp_methods == NULL)
1239                 || !sk_SSL_COMP_push(ssl_comp_methods,comp))
1240                 {
1241                 OPENSSL_free(comp);
1242                 MemCheck_on();
1243                 SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,ERR_R_MALLOC_FAILURE);
1244                 return(1);
1245                 }
1246         else
1247                 {
1248                 MemCheck_on();
1249                 return(0);
1250                 }
1251         }
1252
1253 const char *SSL_COMP_get_name(const COMP_METHOD *comp)
1254         {
1255         if (comp)
1256                 return comp->name;
1257         return NULL;
1258         }
1259
1260 #endif