initialize cipher/digest methods table in SSL_library_init() and hence remove the...
[openssl.git] / ssl / ssl_ciph.c
index c31927706a52ac6f9a2135fc2c8dfecdea266e1c..8c2e1c305e4d3db4f97d93dfcdb42fbb2c683c66 100644 (file)
@@ -165,11 +165,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]=
@@ -235,7 +232,7 @@ static void load_builtin_compressions(void)
        CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
        }
 
-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;
@@ -740,9 +737,18 @@ static int ssl_cipher_process_rulestr(const char *rule_str,
                        if (!found)
                                break;  /* ignore this entry */
 
-                       algorithms |= ca_list[j]->algorithms;
+                       /* 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;
@@ -796,7 +802,7 @@ 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 *co_list = NULL, *head = NULL, *tail = NULL, *curr;
        SSL_CIPHER **ca_list = NULL;
@@ -804,14 +810,8 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
        /*
         * 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
@@ -911,46 +911,18 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
                }
        OPENSSL_free(co_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;
-               }
-
-       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);
@@ -959,13 +931,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_str;
-       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;
@@ -1108,7 +1080,7 @@ char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len)
        return(buf);
        }
 
-char *SSL_CIPHER_get_version(SSL_CIPHER *c)
+char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
        {
        int i;
 
@@ -1123,7 +1095,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);
@@ -1131,7 +1103,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;