Thread-safety fixes
[openssl.git] / ssl / ssl_ciph.c
index 44b50feff77778b997e4b9695d906f03e1793066..545303793f51f8c3c5c3c72a2a1a1c23c4b85718 100644 (file)
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
+/* ====================================================================
+ * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer. 
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. All advertising materials mentioning features or use of this
+ *    software must display the following acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
+ *
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
+ *    endorse or promote products derived from this software without
+ *    prior written permission. For written permission, please contact
+ *    openssl-core@openssl.org.
+ *
+ * 5. Products derived from this software may not be called "OpenSSL"
+ *    nor may "OpenSSL" appear in their names without prior written
+ *    permission of the OpenSSL Project.
+ *
+ * 6. Redistributions of any form whatsoever must retain the following
+ *    acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This product includes cryptographic software written by Eric Young
+ * (eay@cryptsoft.com).  This product includes software written by Tim
+ * Hudson (tjh@cryptsoft.com).
+ *
+ */
 /* ====================================================================
  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  * ECC cipher suite support in OpenSSL originally developed by 
  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  */
+/* ====================================================================
+ * Copyright 2005 Nokia. All rights reserved.
+ *
+ * The portions of the attached software ("Contribution") is developed by
+ * Nokia Corporation and is licensed pursuant to the OpenSSL open source
+ * license.
+ *
+ * The Contribution, originally written by Mika Kousa and Pasi Eronen of
+ * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
+ * support (see RFC 4279) to OpenSSL.
+ *
+ * No patent licenses or other rights except those expressly stated in
+ * the OpenSSL open source license shall be deemed granted or received
+ * expressly, by implication, estoppel, or otherwise.
+ *
+ * No assurances are provided by Nokia that the Contribution does not
+ * infringe the patent or other intellectual property rights of any third
+ * party or that the license provides you with all the necessary rights
+ * to make use of the Contribution.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
+ * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
+ * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
+ * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
+ * OTHERWISE.
+ */
+
 #include <stdio.h>
 #include <openssl/objects.h>
 #include <openssl/comp.h>
 #define SSL_ENC_AES128_IDX     7
 #define SSL_ENC_AES256_IDX     8
 #define SSL_ENC_NUM_IDX                9
+#define SSL_ENC_CAMELLIA128_IDX        9
+#define SSL_ENC_CAMELLIA256_IDX        10
+#undef  SSL_ENC_NUM_IDX
+#define SSL_ENC_NUM_IDX                11
+
 
 static const EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX]={
        NULL,NULL,NULL,NULL,NULL,NULL,
@@ -120,6 +205,7 @@ static const SSL_CIPHER cipher_aliases[]={
        {0,SSL_TXT_kDHd,0,SSL_kDHd,  0,0,0,0,SSL_MKEY_MASK,0},
        {0,SSL_TXT_kEDH,0,SSL_kEDH,  0,0,0,0,SSL_MKEY_MASK,0},
        {0,SSL_TXT_kFZA,0,SSL_kFZA,  0,0,0,0,SSL_MKEY_MASK,0},
+        {0,SSL_TXT_kPSK,0,SSL_kPSK,  0,0,0,0,SSL_MKEY_MASK,0},
        {0,SSL_TXT_DH,  0,SSL_DH,    0,0,0,0,SSL_MKEY_MASK,0},
        {0,SSL_TXT_ECC, 0,(SSL_kECDH|SSL_kECDHE), 0,0,0,0,SSL_MKEY_MASK,0},
        {0,SSL_TXT_EDH, 0,SSL_EDH,   0,0,0,0,SSL_MKEY_MASK|SSL_AUTH_MASK,0},
@@ -127,6 +213,7 @@ static const SSL_CIPHER cipher_aliases[]={
        {0,SSL_TXT_aRSA,0,SSL_aRSA,  0,0,0,0,SSL_AUTH_MASK,0},
        {0,SSL_TXT_aDSS,0,SSL_aDSS,  0,0,0,0,SSL_AUTH_MASK,0},
        {0,SSL_TXT_aFZA,0,SSL_aFZA,  0,0,0,0,SSL_AUTH_MASK,0},
+        {0,SSL_TXT_aPSK,0,SSL_aPSK,  0,0,0,0,SSL_AUTH_MASK,0},
        {0,SSL_TXT_aNULL,0,SSL_aNULL,0,0,0,0,SSL_AUTH_MASK,0},
        {0,SSL_TXT_aDH, 0,SSL_aDH,   0,0,0,0,SSL_AUTH_MASK,0},
        {0,SSL_TXT_DSS, 0,SSL_DSS,   0,0,0,0,SSL_AUTH_MASK,0},
@@ -135,10 +222,13 @@ static const SSL_CIPHER cipher_aliases[]={
        {0,SSL_TXT_3DES,0,SSL_3DES,  0,0,0,0,SSL_ENC_MASK,0},
        {0,SSL_TXT_RC4, 0,SSL_RC4,   0,0,0,0,SSL_ENC_MASK,0},
        {0,SSL_TXT_RC2, 0,SSL_RC2,   0,0,0,0,SSL_ENC_MASK,0},
+#ifndef OPENSSL_NO_IDEA
        {0,SSL_TXT_IDEA,0,SSL_IDEA,  0,0,0,0,SSL_ENC_MASK,0},
+#endif
        {0,SSL_TXT_eNULL,0,SSL_eNULL,0,0,0,0,SSL_ENC_MASK,0},
        {0,SSL_TXT_eFZA,0,SSL_eFZA,  0,0,0,0,SSL_ENC_MASK,0},
        {0,SSL_TXT_AES, 0,SSL_AES,   0,0,0,0,SSL_ENC_MASK,0},
+       {0,SSL_TXT_CAMELLIA,    0,SSL_CAMELLIA,   0,0,0,0,SSL_ENC_MASK,0},
 
        {0,SSL_TXT_MD5, 0,SSL_MD5,   0,0,0,0,SSL_MAC_MASK,0},
        {0,SSL_TXT_SHA1,0,SSL_SHA1,  0,0,0,0,SSL_MAC_MASK,0},
@@ -149,6 +239,7 @@ static const SSL_CIPHER cipher_aliases[]={
        {0,SSL_TXT_RSA, 0,SSL_RSA,   0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK,0},
        {0,SSL_TXT_ADH, 0,SSL_ADH,   0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK,0},
        {0,SSL_TXT_FZA, 0,SSL_FZA,   0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK|SSL_ENC_MASK,0},
+        {0,SSL_TXT_PSK, 0,SSL_PSK,   0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK,0},
 
        {0,SSL_TXT_SSLV2, 0,SSL_SSLV2, 0,0,0,0,SSL_SSL_MASK,0},
        {0,SSL_TXT_SSLV3, 0,SSL_SSLV3, 0,0,0,0,SSL_SSL_MASK,0},
@@ -163,11 +254,8 @@ static const SSL_CIPHER cipher_aliases[]={
        {0,SSL_TXT_HIGH,  0, 0,  SSL_HIGH, 0,0,0,0,SSL_STRONG_MASK},
        };
 
-static int init_ciphers=1;
-
-static void load_ciphers(void)
+void ssl_load_ciphers(void)
        {
-       init_ciphers=0;
        ssl_cipher_methods[SSL_ENC_DES_IDX]= 
                EVP_get_cipherbyname(SN_des_cbc);
        ssl_cipher_methods[SSL_ENC_3DES_IDX]=
@@ -176,12 +264,20 @@ static void load_ciphers(void)
                EVP_get_cipherbyname(SN_rc4);
        ssl_cipher_methods[SSL_ENC_RC2_IDX]= 
                EVP_get_cipherbyname(SN_rc2_cbc);
+#ifndef OPENSSL_NO_IDEA
        ssl_cipher_methods[SSL_ENC_IDEA_IDX]= 
                EVP_get_cipherbyname(SN_idea_cbc);
+#else
+       ssl_cipher_methods[SSL_ENC_IDEA_IDX]= NULL;
+#endif
        ssl_cipher_methods[SSL_ENC_AES128_IDX]=
          EVP_get_cipherbyname(SN_aes_128_cbc);
        ssl_cipher_methods[SSL_ENC_AES256_IDX]=
          EVP_get_cipherbyname(SN_aes_256_cbc);
+       ssl_cipher_methods[SSL_ENC_CAMELLIA128_IDX]=
+         EVP_get_cipherbyname(SN_camellia_128_cbc);
+       ssl_cipher_methods[SSL_ENC_CAMELLIA256_IDX]=
+         EVP_get_cipherbyname(SN_camellia_256_cbc);
 
        ssl_digest_methods[SSL_MD_MD5_IDX]=
                EVP_get_digestbyname(SN_md5);
@@ -189,6 +285,9 @@ static void load_ciphers(void)
                EVP_get_digestbyname(SN_sha1);
        }
 
+
+#ifndef OPENSSL_NO_COMP
+
 static int sk_comp_cmp(const SSL_COMP * const *a,
                        const SSL_COMP * const *b)
        {
@@ -197,39 +296,50 @@ static int sk_comp_cmp(const SSL_COMP * const *a,
 
 static void load_builtin_compressions(void)
        {
-       if (ssl_comp_methods != NULL)
-               return;
+       int got_write_lock = 0;
 
-       CRYPTO_w_lock(CRYPTO_LOCK_SSL);
+       CRYPTO_r_lock(CRYPTO_LOCK_SSL);
        if (ssl_comp_methods == NULL)
                {
-               SSL_COMP *comp = NULL;
-
-               MemCheck_off();
-               ssl_comp_methods=sk_SSL_COMP_new(sk_comp_cmp);
-               if (ssl_comp_methods != NULL)
+               CRYPTO_r_unlock(CRYPTO_LOCK_SSL);
+               CRYPTO_w_lock(CRYPTO_LOCK_SSL);
+               got_write_lock = 1;
+               
+               if (ssl_comp_methods == NULL)
                        {
-                       comp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP));
-                       if (comp != NULL)
+                       SSL_COMP *comp = NULL;
+
+                       MemCheck_off();
+                       ssl_comp_methods=sk_SSL_COMP_new(sk_comp_cmp);
+                       if (ssl_comp_methods != NULL)
                                {
-                               comp->method=COMP_zlib();
-                               if (comp->method
-                                       && comp->method->type == NID_undef)
-                                       OPENSSL_free(comp);
-                               else
+                               comp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP));
+                               if (comp != NULL)
                                        {
-                                       comp->id=SSL_COMP_ZLIB_IDX;
-                                       comp->name=comp->method->name;
-                                       sk_SSL_COMP_push(ssl_comp_methods,comp);
+                                       comp->method=COMP_zlib();
+                                       if (comp->method
+                                               && comp->method->type == NID_undef)
+                                               OPENSSL_free(comp);
+                                       else
+                                               {
+                                               comp->id=SSL_COMP_ZLIB_IDX;
+                                               comp->name=comp->method->name;
+                                               sk_SSL_COMP_push(ssl_comp_methods,comp);
+                                               }
                                        }
                                }
+                       MemCheck_on();
                        }
-               MemCheck_on();
                }
-       CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
+       
+       if (got_write_lock)
+               CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
+       else
+               CRYPTO_r_unlock(CRYPTO_LOCK_SSL);
        }
+#endif
 
-int ssl_cipher_get_evp(SSL_SESSION *s, const EVP_CIPHER **enc,
+int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
             const EVP_MD **md, SSL_COMP **comp)
        {
        int i;
@@ -240,8 +350,9 @@ int ssl_cipher_get_evp(SSL_SESSION *s, const EVP_CIPHER **enc,
        if (comp != NULL)
                {
                SSL_COMP ctmp;
-
+#ifndef OPENSSL_NO_COMP
                load_builtin_compressions();
+#endif
 
                *comp=NULL;
                ctmp.id=s->compress_meth;
@@ -285,6 +396,15 @@ int ssl_cipher_get_evp(SSL_SESSION *s, const EVP_CIPHER **enc,
                default: i=-1; break;
                        }
                break;
+       case SSL_CAMELLIA:
+               switch(c->alg_bits)
+                       {
+               case 128: i=SSL_ENC_CAMELLIA128_IDX; break;
+               case 256: i=SSL_ENC_CAMELLIA256_IDX; break;
+               default: i=-1; break;
+                       }
+               break;
+
        default:
                i= -1;
                break;
@@ -362,6 +482,9 @@ static unsigned long ssl_cipher_get_disabled(void)
 #ifdef OPENSSL_NO_ECDH
        mask |= SSL_kECDH|SSL_kECDHE;
 #endif
+#ifdef OPENSSL_NO_PSK
+       mask |= SSL_kPSK;
+#endif
 #ifdef SSL_FORBID_ENULL
        mask |= SSL_eNULL;
 #endif
@@ -373,6 +496,7 @@ static unsigned long ssl_cipher_get_disabled(void)
        mask |= (ssl_cipher_methods[SSL_ENC_IDEA_IDX] == NULL) ? SSL_IDEA:0;
        mask |= (ssl_cipher_methods[SSL_ENC_eFZA_IDX] == NULL) ? SSL_eFZA:0;
        mask |= (ssl_cipher_methods[SSL_ENC_AES128_IDX] == NULL) ? SSL_AES:0;
+       mask |= (ssl_cipher_methods[SSL_ENC_CAMELLIA128_IDX] == NULL) ? SSL_CAMELLIA:0;
 
        mask |= (ssl_digest_methods[SSL_MD_MD5_IDX ] == NULL) ? SSL_MD5 :0;
        mask |= (ssl_digest_methods[SSL_MD_SHA1_IDX] == NULL) ? SSL_SHA1:0;
@@ -381,10 +505,10 @@ static unsigned long ssl_cipher_get_disabled(void)
        }
 
 static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
-               int num_of_ciphers, unsigned long mask, CIPHER_ORDER *list,
+               int num_of_ciphers, unsigned long mask, CIPHER_ORDER *co_list,
                CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
        {
-       int i, list_num;
+       int i, co_list_num;
        SSL_CIPHER *c;
 
        /*
@@ -395,18 +519,18 @@ static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
         */
 
        /* Get the initial list of ciphers */
-       list_num = 0;   /* actual count of ciphers */
+       co_list_num = 0;        /* actual count of ciphers */
        for (i = 0; i < num_of_ciphers; i++)
                {
                c = ssl_method->get_cipher(i);
                /* drop those that use any of that is not available */
                if ((c != NULL) && c->valid && !(c->algorithms & mask))
                        {
-                       list[list_num].cipher = c;
-                       list[list_num].next = NULL;
-                       list[list_num].prev = NULL;
-                       list[list_num].active = 0;
-                       list_num++;
+                       co_list[co_list_num].cipher = c;
+                       co_list[co_list_num].next = NULL;
+                       co_list[co_list_num].prev = NULL;
+                       co_list[co_list_num].active = 0;
+                       co_list_num++;
 #ifdef KSSL_DEBUG
                        printf("\t%d: %s %lx %lx\n",i,c->name,c->id,c->algorithms);
 #endif /* KSSL_DEBUG */
@@ -419,18 +543,18 @@ static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
        /*
         * Prepare linked list from list entries
         */     
-       for (i = 1; i < list_num - 1; i++)
+       for (i = 1; i < co_list_num - 1; i++)
                {
-               list[i].prev = &(list[i-1]);
-               list[i].next = &(list[i+1]);
+               co_list[i].prev = &(co_list[i-1]);
+               co_list[i].next = &(co_list[i+1]);
                }
-       if (list_num > 0)
+       if (co_list_num > 0)
                {
-               (*head_p) = &(list[0]);
+               (*head_p) = &(co_list[0]);
                (*head_p)->prev = NULL;
-               (*head_p)->next = &(list[1]);
-               (*tail_p) = &(list[list_num - 1]);
-               (*tail_p)->prev = &(list[list_num - 2]);
+               (*head_p)->next = &(co_list[1]);
+               (*tail_p) = &(co_list[co_list_num - 1]);
+               (*tail_p)->prev = &(co_list[co_list_num - 2]);
                (*tail_p)->next = NULL;
                }
        }
@@ -474,9 +598,10 @@ static void ssl_cipher_collect_aliases(SSL_CIPHER **ca_list,
        *ca_curr = NULL;        /* end of list */
        }
 
-static void ssl_cipher_apply_rule(unsigned long algorithms, unsigned long mask,
+static void ssl_cipher_apply_rule(unsigned long cipher_id,
+               unsigned long algorithms, unsigned long mask,
                unsigned long algo_strength, unsigned long mask_strength,
-               int rule, int strength_bits, CIPHER_ORDER *list,
+               int rule, int strength_bits, CIPHER_ORDER *co_list,
                CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
        {
        CIPHER_ORDER *head, *tail, *curr, *curr2, *tail2;
@@ -500,11 +625,19 @@ static void ssl_cipher_apply_rule(unsigned long algorithms, unsigned long mask,
 
                cp = curr->cipher;
 
+               /* If explicit cipher suite match that one only */
+
+               if (cipher_id)
+                       {
+                       if (cp->id != cipher_id)
+                               continue;
+                       }
+
                /*
                 * Selection criteria is either the number of strength_bits
                 * or the algorithm used.
                 */
-               if (strength_bits == -1)
+               else if (strength_bits == -1)
                        {
                        ma = mask & cp->algorithms;
                        ma_s = mask_strength & cp->algo_strength;
@@ -571,8 +704,9 @@ static void ssl_cipher_apply_rule(unsigned long algorithms, unsigned long mask,
        *tail_p = tail;
        }
 
-static int ssl_cipher_strength_sort(CIPHER_ORDER *list, CIPHER_ORDER **head_p,
-                                    CIPHER_ORDER **tail_p)
+static int ssl_cipher_strength_sort(CIPHER_ORDER *co_list,
+                                   CIPHER_ORDER **head_p,
+                                   CIPHER_ORDER **tail_p)
        {
        int max_strength_bits, i, *number_uses;
        CIPHER_ORDER *curr;
@@ -616,20 +750,21 @@ static int ssl_cipher_strength_sort(CIPHER_ORDER *list, CIPHER_ORDER **head_p,
         */
        for (i = max_strength_bits; i >= 0; i--)
                if (number_uses[i] > 0)
-                       ssl_cipher_apply_rule(0, 0, 0, 0, CIPHER_ORD, i,
-                                       list, head_p, tail_p);
+                       ssl_cipher_apply_rule(0, 0, 0, 0, 0, CIPHER_ORD, i,
+                                       co_list, head_p, tail_p);
 
        OPENSSL_free(number_uses);
        return(1);
        }
 
 static int ssl_cipher_process_rulestr(const char *rule_str,
-               CIPHER_ORDER *list, CIPHER_ORDER **head_p,
+               CIPHER_ORDER *co_list, CIPHER_ORDER **head_p,
                CIPHER_ORDER **tail_p, SSL_CIPHER **ca_list)
        {
        unsigned long algorithms, mask, algo_strength, mask_strength;
        const char *l, *start, *buf;
        int j, multi, found, rule, retval, ok, buflen;
+       unsigned long cipher_id;
        char ch;
 
        retval = 1;
@@ -719,6 +854,7 @@ static int ssl_cipher_process_rulestr(const char *rule_str,
                         * use strcmp(), because buf is not '\0' terminated.)
                         */
                         j = found = 0;
+                        cipher_id = 0;
                         while (ca_list[j])
                                {
                                if (!strncmp(buf, ca_list[j]->name, buflen) &&
@@ -733,9 +869,24 @@ static int ssl_cipher_process_rulestr(const char *rule_str,
                        if (!found)
                                break;  /* ignore this entry */
 
-                       algorithms |= ca_list[j]->algorithms;
+                       if (ca_list[j]->valid)
+                               {
+                               cipher_id = ca_list[j]->id;
+                               break;
+                               }
+
+                       /* New algorithms:
+                        *  1 - any old restrictions apply outside new mask
+                        *  2 - any new restrictions apply outside old mask
+                        *  3 - enforce old & new where masks intersect
+                        */
+                       algorithms = (algorithms & ~ca_list[j]->mask) |         /* 1 */
+                                    (ca_list[j]->algorithms & ~mask) |         /* 2 */
+                                    (algorithms & ca_list[j]->algorithms);     /* 3 */
                        mask |= ca_list[j]->mask;
-                       algo_strength |= ca_list[j]->algo_strength;
+                       algo_strength = (algo_strength & ~ca_list[j]->mask_strength) |
+                                       (ca_list[j]->algo_strength & ~mask_strength) |
+                                       (algo_strength & ca_list[j]->algo_strength);
                        mask_strength |= ca_list[j]->mask_strength;
 
                        if (!multi) break;
@@ -749,7 +900,7 @@ static int ssl_cipher_process_rulestr(const char *rule_str,
                        ok = 0;
                        if ((buflen == 8) &&
                                !strncmp(buf, "STRENGTH", 8))
-                               ok = ssl_cipher_strength_sort(list,
+                               ok = ssl_cipher_strength_sort(co_list,
                                        head_p, tail_p);
                        else
                                SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
@@ -767,9 +918,9 @@ static int ssl_cipher_process_rulestr(const char *rule_str,
                        }
                else if (found)
                        {
-                       ssl_cipher_apply_rule(algorithms, mask,
+                       ssl_cipher_apply_rule(cipher_id, algorithms, mask,
                                algo_strength, mask_strength, rule, -1,
-                               list, head_p, tail_p);
+                               co_list, head_p, tail_p);
                        }
                else
                        {
@@ -789,22 +940,16 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
        {
        int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
        unsigned long disabled_mask;
-       STACK_OF(SSL_CIPHER) *cipherstack;
+       STACK_OF(SSL_CIPHER) *cipherstack, *tmp_cipher_list;
        const char *rule_p;
-       CIPHER_ORDER *list = NULL, *head = NULL, *tail = NULL, *curr;
+       CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
        SSL_CIPHER **ca_list = NULL;
 
        /*
         * Return with error if nothing to do.
         */
-       if (rule_str == NULL) return(NULL);
-
-       if (init_ciphers)
-               {
-               CRYPTO_w_lock(CRYPTO_LOCK_SSL);
-               if (init_ciphers) load_ciphers();
-               CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
-               }
+       if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL)
+               return NULL;
 
        /*
         * To reduce the work to do we only want to process the compiled
@@ -821,15 +966,15 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
 #ifdef KSSL_DEBUG
        printf("ssl_create_cipher_list() for %d ciphers\n", num_of_ciphers);
 #endif    /* KSSL_DEBUG */
-       list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
-       if (list == NULL)
+       co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
+       if (co_list == NULL)
                {
                SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
                return(NULL);   /* Failure */
                }
 
        ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, disabled_mask,
-                                  list, &head, &tail);
+                                  co_list, &head, &tail);
 
        /*
         * We also need cipher aliases for selecting based on the rule_str.
@@ -845,7 +990,7 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
                (SSL_CIPHER **)OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
        if (ca_list == NULL)
                {
-               OPENSSL_free(list);
+               OPENSSL_free(co_list);
                SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
                return(NULL);   /* Failure */
                }
@@ -861,21 +1006,21 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
        if (strncmp(rule_str,"DEFAULT",7) == 0)
                {
                ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
-                       list, &head, &tail, ca_list);
+                       co_list, &head, &tail, ca_list);
                rule_p += 7;
                if (*rule_p == ':')
                        rule_p++;
                }
 
        if (ok && (strlen(rule_p) > 0))
-               ok = ssl_cipher_process_rulestr(rule_p, list, &head, &tail,
+               ok = ssl_cipher_process_rulestr(rule_p, co_list, &head, &tail,
                                                ca_list);
 
        OPENSSL_free(ca_list);  /* Not needed anymore */
 
        if (!ok)
                {       /* Rule processing failure */
-               OPENSSL_free(list);
+               OPENSSL_free(co_list);
                return(NULL);
                }
        /*
@@ -884,7 +1029,7 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
         */
        if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL)
                {
-               OPENSSL_free(list);
+               OPENSSL_free(co_list);
                return(NULL);
                }
 
@@ -902,48 +1047,20 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
 #endif
                        }
                }
-       OPENSSL_free(list);     /* Not needed any longer */
-
-       /*
-        * The following passage is a little bit odd. If pointer variables
-        * were supplied to hold STACK_OF(SSL_CIPHER) return information,
-        * the old memory pointed to is free()ed. Then, however, the
-        * cipher_list entry will be assigned just a copy of the returned
-        * cipher stack. For cipher_list_by_id a copy of the cipher stack
-        * will be created. See next comment...
-        */
-       if (cipher_list != NULL)
-               {
-               if (*cipher_list != NULL)
-                       sk_SSL_CIPHER_free(*cipher_list);
-               *cipher_list = cipherstack;
-               }
+       OPENSSL_free(co_list);  /* Not needed any longer */
 
-       if (cipher_list_by_id != NULL)
-               {
-               if (*cipher_list_by_id != NULL)
-                       sk_SSL_CIPHER_free(*cipher_list_by_id);
-               *cipher_list_by_id = sk_SSL_CIPHER_dup(cipherstack);
-               }
-
-       /*
-        * Now it is getting really strange. If something failed during
-        * the previous pointer assignment or if one of the pointers was
-        * not requested, the error condition is met. That might be
-        * discussable. The strange thing is however that in this case
-        * the memory "ret" pointed to is "free()ed" and hence the pointer
-        * cipher_list becomes wild. The memory reserved for
-        * cipher_list_by_id however is not "free()ed" and stays intact.
-        */
-       if (    (cipher_list_by_id == NULL) ||
-               (*cipher_list_by_id == NULL) ||
-               (cipher_list == NULL) ||
-               (*cipher_list == NULL))
+       tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
+       if (tmp_cipher_list == NULL)
                {
                sk_SSL_CIPHER_free(cipherstack);
-               return(NULL);
+               return NULL;
                }
-
+       if (*cipher_list != NULL)
+               sk_SSL_CIPHER_free(*cipher_list);
+       *cipher_list = cipherstack;
+       if (*cipher_list_by_id != NULL)
+               sk_SSL_CIPHER_free(*cipher_list_by_id);
+       *cipher_list_by_id = tmp_cipher_list;
        sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);
 
        return(cipherstack);
@@ -952,13 +1069,13 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
 char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len)
        {
        int is_export,pkl,kl;
-       char *ver,*exp;
-       char *kx,*au,*enc,*mac;
+       const char *ver,*exp_str;
+       const char *kx,*au,*enc,*mac;
        unsigned long alg,alg2,alg_s;
 #ifdef KSSL_DEBUG
-       static char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s AL=%lx\n";
+       static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s AL=%lx\n";
 #else
-       static char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s\n";
+       static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s\n";
 #endif /* KSSL_DEBUG */
 
        alg=cipher->algorithms;
@@ -968,7 +1085,7 @@ char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len)
        is_export=SSL_C_IS_EXPORT(cipher);
        pkl=SSL_C_EXPORT_PKEYLENGTH(cipher);
        kl=SSL_C_EXPORT_KEYLENGTH(cipher);
-       exp=is_export?" export":"";
+       exp_str=is_export?" export":"";
        
        if (alg & SSL_SSLV2)
                ver="SSLv2";
@@ -1002,6 +1119,9 @@ char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len)
        case SSL_kECDHE:
                kx=is_export?"ECDH(<=163)":"ECDH";
                break;
+       case SSL_kPSK:
+               kx="PSK";
+               break;
        default:
                kx="unknown";
                }
@@ -1028,6 +1148,9 @@ char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len)
        case SSL_aECDSA:
                au="ECDSA";
                break;
+       case SSL_aPSK:
+               au="PSK";
+               break;
        default:
                au="unknown";
                break;
@@ -1066,6 +1189,15 @@ char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len)
                default: enc="AES(?""?""?)"; break;
                        }
                break;
+       case SSL_CAMELLIA:
+               switch(cipher->strength_bits)
+                       {
+               case 128: enc="Camellia(128)"; break;
+               case 256: enc="Camellia(256)"; break;
+               default: enc="Camellia(?""?""?)"; break;
+                       }
+               break;
+               
        default:
                enc="unknown";
                break;
@@ -1094,14 +1226,14 @@ char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len)
                return("Buffer too small");
 
 #ifdef KSSL_DEBUG
-       BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp,alg);
+       BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str,alg);
 #else
-       BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp);
+       BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str);
 #endif /* KSSL_DEBUG */
        return(buf);
        }
 
-char *SSL_CIPHER_get_version(SSL_CIPHER *c)
+char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
        {
        int i;
 
@@ -1116,7 +1248,7 @@ char *SSL_CIPHER_get_version(SSL_CIPHER *c)
        }
 
 /* return the actual cipher being used */
-const char *SSL_CIPHER_get_name(SSL_CIPHER *c)
+const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
        {
        if (c != NULL)
                return(c->name);
@@ -1124,7 +1256,7 @@ const char *SSL_CIPHER_get_name(SSL_CIPHER *c)
        }
 
 /* number of bits for symmetric cipher */
-int SSL_CIPHER_get_bits(SSL_CIPHER *c, int *alg_bits)
+int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
        {
        int ret=0;
 
@@ -1152,6 +1284,21 @@ SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n)
        return(NULL);
        }
 
+#ifdef OPENSSL_NO_COMP
+void *SSL_COMP_get_compression_methods(void)
+       {
+       return NULL;
+       }
+int SSL_COMP_add_compression_method(int id, void *cm)
+       {
+       return 1;
+       }
+
+const char *SSL_COMP_get_name(const void *comp)
+       {
+       return NULL;
+       }
+#else
 STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
        {
        load_builtin_compressions();
@@ -1204,3 +1351,12 @@ int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
                return(0);
                }
        }
+
+const char *SSL_COMP_get_name(const COMP_METHOD *comp)
+       {
+       if (comp)
+               return comp->name;
+       return NULL;
+       }
+
+#endif