Submitted by: Bodo Moeller and Adam Langley (Google).
[openssl.git] / ssl / ssl_ciph.c
index 496b0d245bb393e59cc74e7fa169a5419c367da3..5e2d436da39a333069996c0a2c25b65c3fe29366 100644 (file)
  */
 #include <stdio.h>
 #include <openssl/objects.h>
+#ifndef OPENSSL_NO_COMP
 #include <openssl/comp.h>
+#endif
+
 #include "ssl_locl.h"
 
 #define SSL_ENC_DES_IDX                0
 #define SSL_ENC_NULL_IDX       6
 #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
+#define SSL_ENC_SEED_IDX       11
+#define SSL_ENC_NUM_IDX                12
 
 
 static const EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX]={
@@ -196,10 +198,11 @@ static const SSL_CIPHER cipher_aliases[]={
 #ifndef OPENSSL_NO_IDEA
        {0,SSL_TXT_IDEA,0,SSL_IDEA,  0,0,0,0,SSL_ENC_MASK,0},
 #endif
+       {0,SSL_TXT_SEED,0,SSL_SEED,  0,0,0,0,SSL_ENC_MASK,0},
        {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_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},
@@ -222,6 +225,7 @@ static const SSL_CIPHER cipher_aliases[]={
        {0,SSL_TXT_LOW,   0, 0,   SSL_LOW, 0,0,0,0,SSL_STRONG_MASK},
        {0,SSL_TXT_MEDIUM,0, 0,SSL_MEDIUM, 0,0,0,0,SSL_STRONG_MASK},
        {0,SSL_TXT_HIGH,  0, 0,  SSL_HIGH, 0,0,0,0,SSL_STRONG_MASK},
+       {0,SSL_TXT_FIPS,  0, 0,  SSL_FIPS, 0,0,0,0,SSL_FIPS|SSL_STRONG_NONE},
        };
 
 void ssl_load_ciphers(void)
@@ -248,6 +252,8 @@ void ssl_load_ciphers(void)
          EVP_get_cipherbyname(SN_camellia_128_cbc);
        ssl_cipher_methods[SSL_ENC_CAMELLIA256_IDX]=
          EVP_get_cipherbyname(SN_camellia_256_cbc);
+       ssl_cipher_methods[SSL_ENC_SEED_IDX]=
+         EVP_get_cipherbyname(SN_seed_cbc);
 
        ssl_digest_methods[SSL_MD_MD5_IDX]=
                EVP_get_digestbyname(SN_md5);
@@ -374,6 +380,9 @@ int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
                default: i=-1; break;
                        }
                break;
+       case SSL_SEED:
+               i=SSL_ENC_SEED_IDX;
+               break;
 
        default:
                i= -1;
@@ -432,9 +441,18 @@ static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
        *tail=curr;
        }
 
-static unsigned long ssl_cipher_get_disabled(void)
+struct disabled_masks { /* This is a kludge no longer needed with OpenSSL 0.9.9,
+                         * where 128-bit and 256-bit algorithms simply will get
+                         * separate bits. */
+  unsigned long mask; /* everything except m256 */
+  unsigned long m256; /* applies to 256-bit algorithms only */
+};
+
+static struct disabled_masks ssl_cipher_get_disabled(void)
        {
        unsigned long mask;
+       unsigned long m256;
+       struct disabled_masks ret;
 
        mask = SSL_kFZA;
 #ifdef OPENSSL_NO_RSA
@@ -462,18 +480,27 @@ static unsigned long ssl_cipher_get_disabled(void)
        mask |= (ssl_cipher_methods[SSL_ENC_RC2_IDX ] == NULL) ? SSL_RC2 :0;
        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_cipher_methods[SSL_ENC_SEED_IDX] == NULL) ? SSL_SEED: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;
 
-       return(mask);
+       /* finally consider algorithms where mask and m256 differ */
+       m256 = mask;
+       mask |= (ssl_cipher_methods[SSL_ENC_AES128_IDX] == NULL) ? SSL_AES:0;
+       mask |= (ssl_cipher_methods[SSL_ENC_CAMELLIA128_IDX] == NULL) ? SSL_CAMELLIA:0;
+       m256 |= (ssl_cipher_methods[SSL_ENC_AES256_IDX] == NULL) ? SSL_AES:0;
+       m256 |= (ssl_cipher_methods[SSL_ENC_CAMELLIA256_IDX] == NULL) ? SSL_CAMELLIA:0;
+
+       ret.mask = mask;
+       ret.m256 = m256;
+       return ret;
        }
 
 static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
-               int num_of_ciphers, unsigned long mask, CIPHER_ORDER *co_list,
-               CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
+               int num_of_ciphers, unsigned long mask, unsigned long m256,
+               CIPHER_ORDER *co_list, CIPHER_ORDER **head_p,
+               CIPHER_ORDER **tail_p)
        {
        int i, co_list_num;
        SSL_CIPHER *c;
@@ -490,8 +517,14 @@ static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
        for (i = 0; i < num_of_ciphers; i++)
                {
                c = ssl_method->get_cipher(i);
+#define IS_MASKED(c) ((c)->algorithms & (((c)->alg_bits == 256) ? m256 : mask))
                /* drop those that use any of that is not available */
-               if ((c != NULL) && c->valid && !(c->algorithms & mask))
+#ifdef OPENSSL_FIPS
+               if ((c != NULL) && c->valid && !IS_MASKED(c)
+                       && (!FIPS_mode() || (c->algo_strength & SSL_FIPS)))
+#else
+               if ((c != NULL) && c->valid && !IS_MASKED(c))
+#endif
                        {
                        co_list[co_list_num].cipher = c;
                        co_list[co_list_num].next = NULL;
@@ -565,7 +598,7 @@ static void ssl_cipher_collect_aliases(SSL_CIPHER **ca_list,
        *ca_curr = NULL;        /* end of list */
        }
 
-static void ssl_cipher_apply_rule(unsigned long cipher_id,
+static void ssl_cipher_apply_rule(unsigned long cipher_id, unsigned long ssl_version,
                unsigned long algorithms, unsigned long mask,
                unsigned long algo_strength, unsigned long mask_strength,
                int rule, int strength_bits, CIPHER_ORDER *co_list,
@@ -592,9 +625,10 @@ static void ssl_cipher_apply_rule(unsigned long cipher_id,
 
                cp = curr->cipher;
 
-               /* If explicit cipher suite match that one only */
+               /* If explicit cipher suite, match only that one for its own protocol version.
+                * Usual selection criteria will be used for similar ciphersuites from other version! */
 
-               if (cipher_id)
+               if (cipher_id && (cp->algorithms & SSL_SSL_MASK) == ssl_version)
                        {
                        if (cp->id != cipher_id)
                                continue;
@@ -731,7 +765,7 @@ static int ssl_cipher_strength_sort(CIPHER_ORDER *co_list,
         */
        for (i = max_strength_bits; i >= 0; i--)
                if (number_uses[i] > 0)
-                       ssl_cipher_apply_rule(0, 0, 0, 0, 0, CIPHER_ORD, i,
+                       ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, CIPHER_ORD, i,
                                        co_list, head_p, tail_p);
 
        OPENSSL_free(number_uses);
@@ -745,7 +779,7 @@ static int ssl_cipher_process_rulestr(const char *rule_str,
        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 = 0;
+       unsigned long cipher_id = 0, ssl_version = 0;
        char ch;
 
        retval = 1;
@@ -836,6 +870,7 @@ static int ssl_cipher_process_rulestr(const char *rule_str,
                         */
                         j = found = 0;
                         cipher_id = 0;
+                        ssl_version = 0;
                         while (ca_list[j])
                                {
                                if (!strncmp(buf, ca_list[j]->name, buflen) &&
@@ -850,12 +885,6 @@ static int ssl_cipher_process_rulestr(const char *rule_str,
                        if (!found)
                                break;  /* ignore this entry */
 
-                       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
@@ -870,6 +899,14 @@ static int ssl_cipher_process_rulestr(const char *rule_str,
                                        (algo_strength & ca_list[j]->algo_strength);
                        mask_strength |= ca_list[j]->mask_strength;
 
+                       /* explicit ciphersuite found */
+                       if (ca_list[j]->valid)
+                               {
+                               cipher_id = ca_list[j]->id;
+                               ssl_version = ca_list[j]->algorithms & SSL_SSL_MASK;
+                               break;
+                               }
+
                        if (!multi) break;
                        }
 
@@ -894,18 +931,18 @@ static int ssl_cipher_process_rulestr(const char *rule_str,
                         * rest of the command, if any left, until
                         * end or ':' is found.
                         */
-                       while ((*l != '\0') && ITEM_SEP(*l))
+                       while ((*l != '\0') && !ITEM_SEP(*l))
                                l++;
                        }
                else if (found)
                        {
-                       ssl_cipher_apply_rule(cipher_id, algorithms, mask,
+                       ssl_cipher_apply_rule(cipher_id, ssl_version, algorithms, mask,
                                algo_strength, mask_strength, rule, -1,
                                co_list, head_p, tail_p);
                        }
                else
                        {
-                       while ((*l != '\0') && ITEM_SEP(*l))
+                       while ((*l != '\0') && !ITEM_SEP(*l))
                                l++;
                        }
                if (*l == '\0') break; /* done */
@@ -921,6 +958,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;
+       unsigned long disabled_m256;
        STACK_OF(SSL_CIPHER) *cipherstack, *tmp_cipher_list;
        const char *rule_p;
        CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
@@ -936,7 +974,12 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
         * To reduce the work to do we only want to process the compiled
         * in algorithms, so we first get the mask of disabled ciphers.
         */
-       disabled_mask = ssl_cipher_get_disabled();
+       {
+               struct disabled_masks d;
+               d = ssl_cipher_get_disabled();
+               disabled_mask = d.mask;
+               disabled_m256 = d.m256;
+       }
 
        /*
         * Now we have to collect the available ciphers from the compiled
@@ -955,7 +998,7 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
                }
 
        ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, disabled_mask,
-                                  co_list, &head, &tail);
+                                  disabled_m256, co_list, &head, &tail);
 
        /*
         * We also need cipher aliases for selecting based on the rule_str.
@@ -975,8 +1018,8 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
                SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
                return(NULL);   /* Failure */
                }
-       ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, disabled_mask,
-                                  head);
+       ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
+                                  (disabled_mask & disabled_m256), head);
 
        /*
         * If the rule_string begins with DEFAULT, apply the default rule
@@ -1020,7 +1063,11 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
         */
        for (curr = head; curr != NULL; curr = curr->next)
                {
+#ifdef OPENSSL_FIPS
+               if (curr->active && (!FIPS_mode() || curr->cipher->algo_strength & SSL_FIPS))
+#else
                if (curr->active)
+#endif
                        {
                        sk_SSL_CIPHER_push(cipherstack, curr->cipher);
 #ifdef CIPHER_DEBUG
@@ -1042,12 +1089,13 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
        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);
+       (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);
 
+       sk_SSL_CIPHER_sort(*cipher_list_by_id);
        return(cipherstack);
        }
 
-char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len)
+char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
        {
        int is_export,pkl,kl;
        const char *ver,*exp_str;
@@ -1172,7 +1220,10 @@ char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len)
                default: enc="Camellia(?""?""?)"; break;
                        }
                break;
-               
+       case SSL_SEED:
+               enc="SEED(128)";
+               break;
+
        default:
                enc="unknown";
                break;
@@ -1305,7 +1356,7 @@ int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
        comp->method=cm;
        load_builtin_compressions();
        if (ssl_comp_methods
-               && !sk_SSL_COMP_find(ssl_comp_methods,comp))
+               && sk_SSL_COMP_find(ssl_comp_methods,comp) >= 0)
                {
                OPENSSL_free(comp);
                MemCheck_on();