sl@0: /* ssl/ssl_ciph.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: /* ==================================================================== sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. sl@0: * ECC cipher suite support in OpenSSL originally developed by sl@0: * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include "ssl_locl.h" sl@0: sl@0: #define SSL_ENC_DES_IDX 0 sl@0: #define SSL_ENC_3DES_IDX 1 sl@0: #define SSL_ENC_RC4_IDX 2 sl@0: #define SSL_ENC_RC2_IDX 3 sl@0: #define SSL_ENC_IDEA_IDX 4 sl@0: #define SSL_ENC_eFZA_IDX 5 sl@0: #define SSL_ENC_NULL_IDX 6 sl@0: #define SSL_ENC_AES128_IDX 7 sl@0: #define SSL_ENC_AES256_IDX 8 sl@0: #define SSL_ENC_NUM_IDX 9 sl@0: sl@0: #ifndef EMULATOR sl@0: static const EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX]={ sl@0: NULL,NULL,NULL,NULL,NULL,NULL, sl@0: }; sl@0: #else sl@0: GET_STATIC_ARRAY_FROM_TLS(ssl_cipher_methods,ssl_ciph,const EVP_CIPHER *) sl@0: sl@0: #define ssl_cipher_methods (GET_WSD_VAR_NAME(ssl_cipher_methods,ssl_ciph,s)()) sl@0: #endif sl@0: sl@0: #define SSL_COMP_NULL_IDX 0 sl@0: #define SSL_COMP_ZLIB_IDX 1 sl@0: #define SSL_COMP_NUM_IDX 2 sl@0: sl@0: #ifndef EMULATOR sl@0: static STACK_OF(SSL_COMP) *ssl_comp_methods=NULL; sl@0: #else sl@0: GET_STATIC_VAR_FROM_TLS(ssl_comp_methods,ssl_ciph,STACK_OF(SSL_COMP) *) sl@0: sl@0: #define ssl_comp_methods (*GET_WSD_VAR_NAME(ssl_comp_methods,ssl_ciph,s)()) sl@0: #endif sl@0: sl@0: #define SSL_MD_MD5_IDX 0 sl@0: #define SSL_MD_SHA1_IDX 1 sl@0: #define SSL_MD_NUM_IDX 2 sl@0: #ifndef EMULATOR sl@0: static const EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX]={ sl@0: NULL,NULL, sl@0: }; sl@0: #else /* EMULATOR */ sl@0: GET_STATIC_ARRAY_FROM_TLS(ssl_digest_methods,ssl_ciph,const EVP_MD *) sl@0: sl@0: #define ssl_digest_methods (GET_WSD_VAR_NAME(ssl_digest_methods,ssl_ciph,s)()) sl@0: #endif sl@0: #define CIPHER_ADD 1 sl@0: #define CIPHER_KILL 2 sl@0: #define CIPHER_DEL 3 sl@0: #define CIPHER_ORD 4 sl@0: #define CIPHER_SPECIAL 5 sl@0: sl@0: typedef struct cipher_order_st sl@0: { sl@0: SSL_CIPHER *cipher; sl@0: int active; sl@0: int dead; sl@0: struct cipher_order_st *next,*prev; sl@0: } CIPHER_ORDER; sl@0: sl@0: static const SSL_CIPHER cipher_aliases[]={ sl@0: /* Don't include eNULL unless specifically enabled. */ sl@0: /* Don't include ECC in ALL because these ciphers are not yet official. */ sl@0: {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 */ sl@0: /* TODO: COMPLEMENT OF ALL and COMPLEMENT OF DEFAULT do not have ECC cipher suites handled properly. */ sl@0: {0,SSL_TXT_CMPALL,0,SSL_eNULL,0,0,0,0,SSL_ENC_MASK,0}, /* COMPLEMENT OF ALL */ sl@0: {0,SSL_TXT_CMPDEF,0,SSL_ADH, 0,0,0,0,SSL_AUTH_MASK,0}, sl@0: {0,SSL_TXT_kKRB5,0,SSL_kKRB5,0,0,0,0,SSL_MKEY_MASK,0}, /* VRS Kerberos5 */ sl@0: {0,SSL_TXT_kRSA,0,SSL_kRSA, 0,0,0,0,SSL_MKEY_MASK,0}, sl@0: {0,SSL_TXT_kDHr,0,SSL_kDHr, 0,0,0,0,SSL_MKEY_MASK,0}, sl@0: {0,SSL_TXT_kDHd,0,SSL_kDHd, 0,0,0,0,SSL_MKEY_MASK,0}, sl@0: {0,SSL_TXT_kEDH,0,SSL_kEDH, 0,0,0,0,SSL_MKEY_MASK,0}, sl@0: {0,SSL_TXT_kFZA,0,SSL_kFZA, 0,0,0,0,SSL_MKEY_MASK,0}, sl@0: {0,SSL_TXT_DH, 0,SSL_DH, 0,0,0,0,SSL_MKEY_MASK,0}, sl@0: {0,SSL_TXT_ECC, 0,(SSL_kECDH|SSL_kECDHE), 0,0,0,0,SSL_MKEY_MASK,0}, sl@0: {0,SSL_TXT_EDH, 0,SSL_EDH, 0,0,0,0,SSL_MKEY_MASK|SSL_AUTH_MASK,0}, sl@0: {0,SSL_TXT_aKRB5,0,SSL_aKRB5,0,0,0,0,SSL_AUTH_MASK,0}, /* VRS Kerberos5 */ sl@0: {0,SSL_TXT_aRSA,0,SSL_aRSA, 0,0,0,0,SSL_AUTH_MASK,0}, sl@0: {0,SSL_TXT_aDSS,0,SSL_aDSS, 0,0,0,0,SSL_AUTH_MASK,0}, sl@0: {0,SSL_TXT_aFZA,0,SSL_aFZA, 0,0,0,0,SSL_AUTH_MASK,0}, sl@0: {0,SSL_TXT_aNULL,0,SSL_aNULL,0,0,0,0,SSL_AUTH_MASK,0}, sl@0: {0,SSL_TXT_aDH, 0,SSL_aDH, 0,0,0,0,SSL_AUTH_MASK,0}, sl@0: {0,SSL_TXT_DSS, 0,SSL_DSS, 0,0,0,0,SSL_AUTH_MASK,0}, sl@0: sl@0: {0,SSL_TXT_DES, 0,SSL_DES, 0,0,0,0,SSL_ENC_MASK,0}, sl@0: {0,SSL_TXT_3DES,0,SSL_3DES, 0,0,0,0,SSL_ENC_MASK,0}, sl@0: {0,SSL_TXT_RC4, 0,SSL_RC4, 0,0,0,0,SSL_ENC_MASK,0}, sl@0: {0,SSL_TXT_RC2, 0,SSL_RC2, 0,0,0,0,SSL_ENC_MASK,0}, sl@0: #ifndef OPENSSL_NO_IDEA sl@0: {0,SSL_TXT_IDEA,0,SSL_IDEA, 0,0,0,0,SSL_ENC_MASK,0}, sl@0: #endif sl@0: {0,SSL_TXT_eNULL,0,SSL_eNULL,0,0,0,0,SSL_ENC_MASK,0}, sl@0: {0,SSL_TXT_eFZA,0,SSL_eFZA, 0,0,0,0,SSL_ENC_MASK,0}, sl@0: {0,SSL_TXT_AES, 0,SSL_AES, 0,0,0,0,SSL_ENC_MASK,0}, sl@0: sl@0: {0,SSL_TXT_MD5, 0,SSL_MD5, 0,0,0,0,SSL_MAC_MASK,0}, sl@0: {0,SSL_TXT_SHA1,0,SSL_SHA1, 0,0,0,0,SSL_MAC_MASK,0}, sl@0: {0,SSL_TXT_SHA, 0,SSL_SHA, 0,0,0,0,SSL_MAC_MASK,0}, sl@0: sl@0: {0,SSL_TXT_NULL,0,SSL_NULL, 0,0,0,0,SSL_ENC_MASK,0}, sl@0: {0,SSL_TXT_KRB5,0,SSL_KRB5, 0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK,0}, sl@0: {0,SSL_TXT_RSA, 0,SSL_RSA, 0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK,0}, sl@0: {0,SSL_TXT_ADH, 0,SSL_ADH, 0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK,0}, sl@0: {0,SSL_TXT_FZA, 0,SSL_FZA, 0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK|SSL_ENC_MASK,0}, sl@0: sl@0: {0,SSL_TXT_SSLV2, 0,SSL_SSLV2, 0,0,0,0,SSL_SSL_MASK,0}, sl@0: {0,SSL_TXT_SSLV3, 0,SSL_SSLV3, 0,0,0,0,SSL_SSL_MASK,0}, sl@0: {0,SSL_TXT_TLSV1, 0,SSL_TLSV1, 0,0,0,0,SSL_SSL_MASK,0}, sl@0: sl@0: {0,SSL_TXT_EXP ,0, 0,SSL_EXPORT, 0,0,0,0,SSL_EXP_MASK}, sl@0: {0,SSL_TXT_EXPORT,0, 0,SSL_EXPORT, 0,0,0,0,SSL_EXP_MASK}, sl@0: {0,SSL_TXT_EXP40, 0, 0, SSL_EXP40, 0,0,0,0,SSL_STRONG_MASK}, sl@0: {0,SSL_TXT_EXP56, 0, 0, SSL_EXP56, 0,0,0,0,SSL_STRONG_MASK}, sl@0: {0,SSL_TXT_LOW, 0, 0, SSL_LOW, 0,0,0,0,SSL_STRONG_MASK}, sl@0: {0,SSL_TXT_MEDIUM,0, 0,SSL_MEDIUM, 0,0,0,0,SSL_STRONG_MASK}, sl@0: {0,SSL_TXT_HIGH, 0, 0, SSL_HIGH, 0,0,0,0,SSL_STRONG_MASK}, sl@0: }; sl@0: sl@0: void ssl_load_ciphers(void) sl@0: { sl@0: ssl_cipher_methods[SSL_ENC_DES_IDX]= sl@0: EVP_get_cipherbyname(SN_des_cbc); sl@0: ssl_cipher_methods[SSL_ENC_3DES_IDX]= sl@0: EVP_get_cipherbyname(SN_des_ede3_cbc); sl@0: ssl_cipher_methods[SSL_ENC_RC4_IDX]= sl@0: EVP_get_cipherbyname(SN_rc4); sl@0: ssl_cipher_methods[SSL_ENC_RC2_IDX]= sl@0: EVP_get_cipherbyname(SN_rc2_cbc); sl@0: #ifndef OPENSSL_NO_IDEA sl@0: ssl_cipher_methods[SSL_ENC_IDEA_IDX]= sl@0: EVP_get_cipherbyname(SN_idea_cbc); sl@0: #else sl@0: ssl_cipher_methods[SSL_ENC_IDEA_IDX]= NULL; sl@0: #endif sl@0: ssl_cipher_methods[SSL_ENC_AES128_IDX]= sl@0: EVP_get_cipherbyname(SN_aes_128_cbc); sl@0: ssl_cipher_methods[SSL_ENC_AES256_IDX]= sl@0: EVP_get_cipherbyname(SN_aes_256_cbc); sl@0: sl@0: ssl_digest_methods[SSL_MD_MD5_IDX]= sl@0: EVP_get_digestbyname(SN_md5); sl@0: ssl_digest_methods[SSL_MD_SHA1_IDX]= sl@0: EVP_get_digestbyname(SN_sha1); sl@0: } sl@0: sl@0: sl@0: #ifndef OPENSSL_NO_COMP sl@0: sl@0: static int sk_comp_cmp(const SSL_COMP * const *a, sl@0: const SSL_COMP * const *b) sl@0: { sl@0: return((*a)->id-(*b)->id); sl@0: } sl@0: sl@0: static void load_builtin_compressions(void) sl@0: { sl@0: int got_write_lock = 0; sl@0: sl@0: CRYPTO_r_lock(CRYPTO_LOCK_SSL); sl@0: if (ssl_comp_methods == NULL) sl@0: { sl@0: CRYPTO_r_unlock(CRYPTO_LOCK_SSL); sl@0: CRYPTO_w_lock(CRYPTO_LOCK_SSL); sl@0: got_write_lock = 1; sl@0: sl@0: if (ssl_comp_methods == NULL) sl@0: { sl@0: SSL_COMP *comp = NULL; sl@0: sl@0: MemCheck_off(); sl@0: ssl_comp_methods=sk_SSL_COMP_new(sk_comp_cmp); sl@0: if (ssl_comp_methods != NULL) sl@0: { sl@0: comp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP)); sl@0: if (comp != NULL) sl@0: { sl@0: comp->method=COMP_zlib(); sl@0: if (comp->method sl@0: && comp->method->type == NID_undef) sl@0: OPENSSL_free(comp); sl@0: else sl@0: { sl@0: comp->id=SSL_COMP_ZLIB_IDX; sl@0: comp->name=comp->method->name; sl@0: sk_SSL_COMP_push(ssl_comp_methods,comp); sl@0: } sl@0: } sl@0: } sl@0: MemCheck_on(); sl@0: } sl@0: } sl@0: sl@0: if (got_write_lock) sl@0: CRYPTO_w_unlock(CRYPTO_LOCK_SSL); sl@0: else sl@0: CRYPTO_r_unlock(CRYPTO_LOCK_SSL); sl@0: } sl@0: #endif sl@0: sl@0: int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc, sl@0: const EVP_MD **md, SSL_COMP **comp) sl@0: { sl@0: int i; sl@0: SSL_CIPHER *c; sl@0: sl@0: c=s->cipher; sl@0: if (c == NULL) return(0); sl@0: if (comp != NULL) sl@0: { sl@0: SSL_COMP ctmp; sl@0: #ifndef OPENSSL_NO_COMP sl@0: load_builtin_compressions(); sl@0: #endif sl@0: sl@0: *comp=NULL; sl@0: ctmp.id=s->compress_meth; sl@0: if (ssl_comp_methods != NULL) sl@0: { sl@0: i=sk_SSL_COMP_find(ssl_comp_methods,&ctmp); sl@0: if (i >= 0) sl@0: *comp=sk_SSL_COMP_value(ssl_comp_methods,i); sl@0: else sl@0: *comp=NULL; sl@0: } sl@0: } sl@0: sl@0: if ((enc == NULL) || (md == NULL)) return(0); sl@0: sl@0: switch (c->algorithms & SSL_ENC_MASK) sl@0: { sl@0: case SSL_DES: sl@0: i=SSL_ENC_DES_IDX; sl@0: break; sl@0: case SSL_3DES: sl@0: i=SSL_ENC_3DES_IDX; sl@0: break; sl@0: case SSL_RC4: sl@0: i=SSL_ENC_RC4_IDX; sl@0: break; sl@0: case SSL_RC2: sl@0: i=SSL_ENC_RC2_IDX; sl@0: break; sl@0: case SSL_IDEA: sl@0: i=SSL_ENC_IDEA_IDX; sl@0: break; sl@0: case SSL_eNULL: sl@0: i=SSL_ENC_NULL_IDX; sl@0: break; sl@0: case SSL_AES: sl@0: switch(c->alg_bits) sl@0: { sl@0: case 128: i=SSL_ENC_AES128_IDX; break; sl@0: case 256: i=SSL_ENC_AES256_IDX; break; sl@0: default: i=-1; break; sl@0: } sl@0: break; sl@0: default: sl@0: i= -1; sl@0: break; sl@0: } sl@0: sl@0: if ((i < 0) || (i > SSL_ENC_NUM_IDX)) sl@0: *enc=NULL; sl@0: else sl@0: { sl@0: if (i == SSL_ENC_NULL_IDX) sl@0: *enc=EVP_enc_null(); sl@0: else sl@0: *enc=ssl_cipher_methods[i]; sl@0: } sl@0: sl@0: switch (c->algorithms & SSL_MAC_MASK) sl@0: { sl@0: case SSL_MD5: sl@0: i=SSL_MD_MD5_IDX; sl@0: break; sl@0: case SSL_SHA1: sl@0: i=SSL_MD_SHA1_IDX; sl@0: break; sl@0: default: sl@0: i= -1; sl@0: break; sl@0: } sl@0: if ((i < 0) || (i > SSL_MD_NUM_IDX)) sl@0: *md=NULL; sl@0: else sl@0: *md=ssl_digest_methods[i]; sl@0: sl@0: if ((*enc != NULL) && (*md != NULL)) sl@0: return(1); sl@0: else sl@0: return(0); sl@0: } sl@0: sl@0: #define ITEM_SEP(a) \ sl@0: (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ',')) sl@0: sl@0: static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr, sl@0: CIPHER_ORDER **tail) sl@0: { sl@0: if (curr == *tail) return; sl@0: if (curr == *head) sl@0: *head=curr->next; sl@0: if (curr->prev != NULL) sl@0: curr->prev->next=curr->next; sl@0: if (curr->next != NULL) /* should always be true */ sl@0: curr->next->prev=curr->prev; sl@0: (*tail)->next=curr; sl@0: curr->prev= *tail; sl@0: curr->next=NULL; sl@0: *tail=curr; sl@0: } sl@0: sl@0: static unsigned long ssl_cipher_get_disabled(void) sl@0: { sl@0: unsigned long mask; sl@0: sl@0: mask = SSL_kFZA; sl@0: #ifdef OPENSSL_NO_RSA sl@0: mask |= SSL_aRSA|SSL_kRSA; sl@0: #endif sl@0: #ifdef OPENSSL_NO_DSA sl@0: mask |= SSL_aDSS; sl@0: #endif sl@0: #ifdef OPENSSL_NO_DH sl@0: mask |= SSL_kDHr|SSL_kDHd|SSL_kEDH|SSL_aDH; sl@0: #endif sl@0: #ifdef OPENSSL_NO_KRB5 sl@0: mask |= SSL_kKRB5|SSL_aKRB5; sl@0: #endif sl@0: #ifdef OPENSSL_NO_ECDH sl@0: mask |= SSL_kECDH|SSL_kECDHE; sl@0: #endif sl@0: #ifdef SSL_FORBID_ENULL sl@0: mask |= SSL_eNULL; sl@0: #endif sl@0: sl@0: mask |= (ssl_cipher_methods[SSL_ENC_DES_IDX ] == NULL) ? SSL_DES :0; sl@0: mask |= (ssl_cipher_methods[SSL_ENC_3DES_IDX] == NULL) ? SSL_3DES:0; sl@0: mask |= (ssl_cipher_methods[SSL_ENC_RC4_IDX ] == NULL) ? SSL_RC4 :0; sl@0: mask |= (ssl_cipher_methods[SSL_ENC_RC2_IDX ] == NULL) ? SSL_RC2 :0; sl@0: mask |= (ssl_cipher_methods[SSL_ENC_IDEA_IDX] == NULL) ? SSL_IDEA:0; sl@0: mask |= (ssl_cipher_methods[SSL_ENC_eFZA_IDX] == NULL) ? SSL_eFZA:0; sl@0: mask |= (ssl_cipher_methods[SSL_ENC_AES128_IDX] == NULL) ? SSL_AES:0; sl@0: sl@0: mask |= (ssl_digest_methods[SSL_MD_MD5_IDX ] == NULL) ? SSL_MD5 :0; sl@0: mask |= (ssl_digest_methods[SSL_MD_SHA1_IDX] == NULL) ? SSL_SHA1:0; sl@0: sl@0: return(mask); sl@0: } sl@0: sl@0: static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method, sl@0: int num_of_ciphers, unsigned long mask, CIPHER_ORDER *co_list, sl@0: CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) sl@0: { sl@0: int i, co_list_num; sl@0: SSL_CIPHER *c; sl@0: sl@0: /* sl@0: * We have num_of_ciphers descriptions compiled in, depending on the sl@0: * method selected (SSLv2 and/or SSLv3, TLSv1 etc). sl@0: * These will later be sorted in a linked list with at most num sl@0: * entries. sl@0: */ sl@0: sl@0: /* Get the initial list of ciphers */ sl@0: co_list_num = 0; /* actual count of ciphers */ sl@0: for (i = 0; i < num_of_ciphers; i++) sl@0: { sl@0: c = ssl_method->get_cipher(i); sl@0: /* drop those that use any of that is not available */ sl@0: if ((c != NULL) && c->valid && !(c->algorithms & mask)) sl@0: { sl@0: co_list[co_list_num].cipher = c; sl@0: co_list[co_list_num].next = NULL; sl@0: co_list[co_list_num].prev = NULL; sl@0: co_list[co_list_num].active = 0; sl@0: co_list_num++; sl@0: #ifdef KSSL_DEBUG sl@0: printf("\t%d: %s %lx %lx\n",i,c->name,c->id,c->algorithms); sl@0: #endif /* KSSL_DEBUG */ sl@0: /* sl@0: if (!sk_push(ca_list,(char *)c)) goto err; sl@0: */ sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Prepare linked list from list entries sl@0: */ sl@0: for (i = 1; i < co_list_num - 1; i++) sl@0: { sl@0: co_list[i].prev = &(co_list[i-1]); sl@0: co_list[i].next = &(co_list[i+1]); sl@0: } sl@0: if (co_list_num > 0) sl@0: { sl@0: (*head_p) = &(co_list[0]); sl@0: (*head_p)->prev = NULL; sl@0: (*head_p)->next = &(co_list[1]); sl@0: (*tail_p) = &(co_list[co_list_num - 1]); sl@0: (*tail_p)->prev = &(co_list[co_list_num - 2]); sl@0: (*tail_p)->next = NULL; sl@0: } sl@0: } sl@0: sl@0: static void ssl_cipher_collect_aliases(SSL_CIPHER **ca_list, sl@0: int num_of_group_aliases, unsigned long mask, sl@0: CIPHER_ORDER *head) sl@0: { sl@0: CIPHER_ORDER *ciph_curr; sl@0: SSL_CIPHER **ca_curr; sl@0: int i; sl@0: sl@0: /* sl@0: * First, add the real ciphers as already collected sl@0: */ sl@0: ciph_curr = head; sl@0: ca_curr = ca_list; sl@0: while (ciph_curr != NULL) sl@0: { sl@0: *ca_curr = ciph_curr->cipher; sl@0: ca_curr++; sl@0: ciph_curr = ciph_curr->next; sl@0: } sl@0: sl@0: /* sl@0: * Now we add the available ones from the cipher_aliases[] table. sl@0: * They represent either an algorithm, that must be fully sl@0: * supported (not match any bit in mask) or represent a cipher sl@0: * strength value (will be added in any case because algorithms=0). sl@0: */ sl@0: for (i = 0; i < num_of_group_aliases; i++) sl@0: { sl@0: if ((i == 0) || /* always fetch "ALL" */ sl@0: !(cipher_aliases[i].algorithms & mask)) sl@0: { sl@0: *ca_curr = (SSL_CIPHER *)(cipher_aliases + i); sl@0: ca_curr++; sl@0: } sl@0: } sl@0: sl@0: *ca_curr = NULL; /* end of list */ sl@0: } sl@0: sl@0: static void ssl_cipher_apply_rule(unsigned long cipher_id, unsigned long ssl_version, sl@0: unsigned long algorithms, unsigned long mask, sl@0: unsigned long algo_strength, unsigned long mask_strength, sl@0: int rule, int strength_bits, CIPHER_ORDER *co_list, sl@0: CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) sl@0: { sl@0: CIPHER_ORDER *head, *tail, *curr, *curr2, *tail2; sl@0: SSL_CIPHER *cp; sl@0: unsigned long ma, ma_s; sl@0: sl@0: #ifdef CIPHER_DEBUG sl@0: printf("Applying rule %d with %08lx %08lx %08lx %08lx (%d)\n", sl@0: rule, algorithms, mask, algo_strength, mask_strength, sl@0: strength_bits); sl@0: #endif sl@0: sl@0: curr = head = *head_p; sl@0: curr2 = head; sl@0: tail2 = tail = *tail_p; sl@0: for (;;) sl@0: { sl@0: if ((curr == NULL) || (curr == tail2)) break; sl@0: curr = curr2; sl@0: curr2 = curr->next; sl@0: sl@0: cp = curr->cipher; sl@0: sl@0: /* If explicit cipher suite, match only that one for its own protocol version. sl@0: * Usual selection criteria will be used for similar ciphersuites from other version! */ sl@0: sl@0: if (cipher_id && (cp->algorithms & SSL_SSL_MASK) == ssl_version) sl@0: { sl@0: if (cp->id != cipher_id) sl@0: continue; sl@0: } sl@0: sl@0: /* sl@0: * Selection criteria is either the number of strength_bits sl@0: * or the algorithm used. sl@0: */ sl@0: else if (strength_bits == -1) sl@0: { sl@0: ma = mask & cp->algorithms; sl@0: ma_s = mask_strength & cp->algo_strength; sl@0: sl@0: #ifdef CIPHER_DEBUG sl@0: printf("\nName: %s:\nAlgo = %08lx Algo_strength = %08lx\nMask = %08lx Mask_strength %08lx\n", cp->name, cp->algorithms, cp->algo_strength, mask, mask_strength); sl@0: printf("ma = %08lx ma_s %08lx, ma&algo=%08lx, ma_s&algos=%08lx\n", ma, ma_s, ma&algorithms, ma_s&algo_strength); sl@0: #endif sl@0: /* sl@0: * Select: if none of the mask bit was met from the sl@0: * cipher or not all of the bits were met, the sl@0: * selection does not apply. sl@0: */ sl@0: if (((ma == 0) && (ma_s == 0)) || sl@0: ((ma & algorithms) != ma) || sl@0: ((ma_s & algo_strength) != ma_s)) sl@0: continue; /* does not apply */ sl@0: } sl@0: else if (strength_bits != cp->strength_bits) sl@0: continue; /* does not apply */ sl@0: sl@0: #ifdef CIPHER_DEBUG sl@0: printf("Action = %d\n", rule); sl@0: #endif sl@0: sl@0: /* add the cipher if it has not been added yet. */ sl@0: if (rule == CIPHER_ADD) sl@0: { sl@0: if (!curr->active) sl@0: { sl@0: int add_this_cipher = 1; sl@0: sl@0: if (((cp->algorithms & (SSL_kECDHE|SSL_kECDH|SSL_aECDSA)) != 0)) sl@0: { sl@0: /* Make sure "ECCdraft" ciphersuites are activated only if sl@0: * *explicitly* requested, but not implicitly (such as sl@0: * as part of the "AES" alias). */ sl@0: sl@0: add_this_cipher = (mask & (SSL_kECDHE|SSL_kECDH|SSL_aECDSA)) != 0 || cipher_id != 0; sl@0: } sl@0: sl@0: if (add_this_cipher) sl@0: { sl@0: ll_append_tail(&head, curr, &tail); sl@0: curr->active = 1; sl@0: } sl@0: } sl@0: } sl@0: /* Move the added cipher to this location */ sl@0: else if (rule == CIPHER_ORD) sl@0: { sl@0: if (curr->active) sl@0: { sl@0: ll_append_tail(&head, curr, &tail); sl@0: } sl@0: } sl@0: else if (rule == CIPHER_DEL) sl@0: curr->active = 0; sl@0: else if (rule == CIPHER_KILL) sl@0: { sl@0: if (head == curr) sl@0: head = curr->next; sl@0: else sl@0: curr->prev->next = curr->next; sl@0: if (tail == curr) sl@0: tail = curr->prev; sl@0: curr->active = 0; sl@0: if (curr->next != NULL) sl@0: curr->next->prev = curr->prev; sl@0: if (curr->prev != NULL) sl@0: curr->prev->next = curr->next; sl@0: curr->next = NULL; sl@0: curr->prev = NULL; sl@0: } sl@0: } sl@0: sl@0: *head_p = head; sl@0: *tail_p = tail; sl@0: } sl@0: sl@0: static int ssl_cipher_strength_sort(CIPHER_ORDER *co_list, sl@0: CIPHER_ORDER **head_p, sl@0: CIPHER_ORDER **tail_p) sl@0: { sl@0: int max_strength_bits, i, *number_uses; sl@0: CIPHER_ORDER *curr; sl@0: sl@0: /* sl@0: * This routine sorts the ciphers with descending strength. The sorting sl@0: * must keep the pre-sorted sequence, so we apply the normal sorting sl@0: * routine as '+' movement to the end of the list. sl@0: */ sl@0: max_strength_bits = 0; sl@0: curr = *head_p; sl@0: while (curr != NULL) sl@0: { sl@0: if (curr->active && sl@0: (curr->cipher->strength_bits > max_strength_bits)) sl@0: max_strength_bits = curr->cipher->strength_bits; sl@0: curr = curr->next; sl@0: } sl@0: sl@0: number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int)); sl@0: if (!number_uses) sl@0: { sl@0: SSLerr(SSL_F_SSL_CIPHER_STRENGTH_SORT,ERR_R_MALLOC_FAILURE); sl@0: return(0); sl@0: } sl@0: memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int)); sl@0: sl@0: /* sl@0: * Now find the strength_bits values actually used sl@0: */ sl@0: curr = *head_p; sl@0: while (curr != NULL) sl@0: { sl@0: if (curr->active) sl@0: number_uses[curr->cipher->strength_bits]++; sl@0: curr = curr->next; sl@0: } sl@0: /* sl@0: * Go through the list of used strength_bits values in descending sl@0: * order. sl@0: */ sl@0: for (i = max_strength_bits; i >= 0; i--) sl@0: if (number_uses[i] > 0) sl@0: ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, CIPHER_ORD, i, sl@0: co_list, head_p, tail_p); sl@0: sl@0: OPENSSL_free(number_uses); sl@0: return(1); sl@0: } sl@0: sl@0: static int ssl_cipher_process_rulestr(const char *rule_str, sl@0: CIPHER_ORDER *co_list, CIPHER_ORDER **head_p, sl@0: CIPHER_ORDER **tail_p, SSL_CIPHER **ca_list) sl@0: { sl@0: unsigned long algorithms, mask, algo_strength, mask_strength; sl@0: const char *l, *start, *buf; sl@0: int j, multi, found, rule, retval, ok, buflen; sl@0: unsigned long cipher_id = 0, ssl_version = 0; sl@0: char ch; sl@0: sl@0: retval = 1; sl@0: l = rule_str; sl@0: for (;;) sl@0: { sl@0: ch = *l; sl@0: sl@0: if (ch == '\0') sl@0: break; /* done */ sl@0: if (ch == '-') sl@0: { rule = CIPHER_DEL; l++; } sl@0: else if (ch == '+') sl@0: { rule = CIPHER_ORD; l++; } sl@0: else if (ch == '!') sl@0: { rule = CIPHER_KILL; l++; } sl@0: else if (ch == '@') sl@0: { rule = CIPHER_SPECIAL; l++; } sl@0: else sl@0: { rule = CIPHER_ADD; } sl@0: sl@0: if (ITEM_SEP(ch)) sl@0: { sl@0: l++; sl@0: continue; sl@0: } sl@0: sl@0: algorithms = mask = algo_strength = mask_strength = 0; sl@0: sl@0: start=l; sl@0: for (;;) sl@0: { sl@0: ch = *l; sl@0: buf = l; sl@0: buflen = 0; sl@0: #ifndef CHARSET_EBCDIC sl@0: while ( ((ch >= 'A') && (ch <= 'Z')) || sl@0: ((ch >= '0') && (ch <= '9')) || sl@0: ((ch >= 'a') && (ch <= 'z')) || sl@0: (ch == '-')) sl@0: #else sl@0: while ( isalnum(ch) || (ch == '-')) sl@0: #endif sl@0: { sl@0: ch = *(++l); sl@0: buflen++; sl@0: } sl@0: sl@0: if (buflen == 0) sl@0: { sl@0: /* sl@0: * We hit something we cannot deal with, sl@0: * it is no command or separator nor sl@0: * alphanumeric, so we call this an error. sl@0: */ sl@0: SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR, sl@0: SSL_R_INVALID_COMMAND); sl@0: retval = found = 0; sl@0: l++; sl@0: break; sl@0: } sl@0: sl@0: if (rule == CIPHER_SPECIAL) sl@0: { sl@0: found = 0; /* unused -- avoid compiler warning */ sl@0: break; /* special treatment */ sl@0: } sl@0: sl@0: /* check for multi-part specification */ sl@0: if (ch == '+') sl@0: { sl@0: multi=1; sl@0: l++; sl@0: } sl@0: else sl@0: multi=0; sl@0: sl@0: /* sl@0: * Now search for the cipher alias in the ca_list. Be careful sl@0: * with the strncmp, because the "buflen" limitation sl@0: * will make the rule "ADH:SOME" and the cipher sl@0: * "ADH-MY-CIPHER" look like a match for buflen=3. sl@0: * So additionally check whether the cipher name found sl@0: * has the correct length. We can save a strlen() call: sl@0: * just checking for the '\0' at the right place is sl@0: * sufficient, we have to strncmp() anyway. (We cannot sl@0: * use strcmp(), because buf is not '\0' terminated.) sl@0: */ sl@0: j = found = 0; sl@0: cipher_id = 0; sl@0: ssl_version = 0; sl@0: sl@0: while (ca_list[j]) sl@0: { sl@0: if (!strncmp(buf, ca_list[j]->name, buflen) && sl@0: (ca_list[j]->name[buflen] == '\0')) sl@0: { sl@0: found = 1; sl@0: break; sl@0: } sl@0: else sl@0: j++; sl@0: } sl@0: if (!found) sl@0: break; /* ignore this entry */ sl@0: sl@0: /* New algorithms: sl@0: * 1 - any old restrictions apply outside new mask sl@0: * 2 - any new restrictions apply outside old mask sl@0: * 3 - enforce old & new where masks intersect sl@0: */ sl@0: algorithms = (algorithms & ~ca_list[j]->mask) | /* 1 */ sl@0: (ca_list[j]->algorithms & ~mask) | /* 2 */ sl@0: (algorithms & ca_list[j]->algorithms); /* 3 */ sl@0: mask |= ca_list[j]->mask; sl@0: algo_strength = (algo_strength & ~ca_list[j]->mask_strength) | sl@0: (ca_list[j]->algo_strength & ~mask_strength) | sl@0: (algo_strength & ca_list[j]->algo_strength); sl@0: mask_strength |= ca_list[j]->mask_strength; sl@0: sl@0: /* explicit ciphersuite found */ sl@0: if (ca_list[j]->valid) sl@0: { sl@0: cipher_id = ca_list[j]->id; sl@0: ssl_version = ca_list[j]->algorithms & SSL_SSL_MASK; sl@0: break; sl@0: } sl@0: sl@0: if (!multi) break; sl@0: } sl@0: sl@0: /* sl@0: * Ok, we have the rule, now apply it sl@0: */ sl@0: if (rule == CIPHER_SPECIAL) sl@0: { /* special command */ sl@0: ok = 0; sl@0: if ((buflen == 8) && sl@0: !strncmp(buf, "STRENGTH", 8)) sl@0: ok = ssl_cipher_strength_sort(co_list, sl@0: head_p, tail_p); sl@0: else sl@0: SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR, sl@0: SSL_R_INVALID_COMMAND); sl@0: if (ok == 0) sl@0: retval = 0; sl@0: /* sl@0: * We do not support any "multi" options sl@0: * together with "@", so throw away the sl@0: * rest of the command, if any left, until sl@0: * end or ':' is found. sl@0: */ sl@0: while ((*l != '\0') && !ITEM_SEP(*l)) sl@0: l++; sl@0: } sl@0: else if (found) sl@0: { sl@0: ssl_cipher_apply_rule(cipher_id, ssl_version, algorithms, mask, sl@0: algo_strength, mask_strength, rule, -1, sl@0: co_list, head_p, tail_p); sl@0: } sl@0: else sl@0: { sl@0: while ((*l != '\0') && !ITEM_SEP(*l)) sl@0: l++; sl@0: } sl@0: if (*l == '\0') break; /* done */ sl@0: } sl@0: sl@0: return(retval); sl@0: } sl@0: sl@0: STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method, sl@0: STACK_OF(SSL_CIPHER) **cipher_list, sl@0: STACK_OF(SSL_CIPHER) **cipher_list_by_id, sl@0: const char *rule_str) sl@0: { sl@0: int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases; sl@0: unsigned long disabled_mask; sl@0: STACK_OF(SSL_CIPHER) *cipherstack, *tmp_cipher_list; sl@0: const char *rule_p; sl@0: CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr; sl@0: SSL_CIPHER **ca_list = NULL; sl@0: sl@0: /* sl@0: * Return with error if nothing to do. sl@0: */ sl@0: if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL) sl@0: return NULL; sl@0: sl@0: /* sl@0: * To reduce the work to do we only want to process the compiled sl@0: * in algorithms, so we first get the mask of disabled ciphers. sl@0: */ sl@0: disabled_mask = ssl_cipher_get_disabled(); sl@0: sl@0: /* sl@0: * Now we have to collect the available ciphers from the compiled sl@0: * in ciphers. We cannot get more than the number compiled in, so sl@0: * it is used for allocation. sl@0: */ sl@0: num_of_ciphers = ssl_method->num_ciphers(); sl@0: #ifdef KSSL_DEBUG sl@0: printf("ssl_create_cipher_list() for %d ciphers\n", num_of_ciphers); sl@0: #endif /* KSSL_DEBUG */ sl@0: co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers); sl@0: if (co_list == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE); sl@0: return(NULL); /* Failure */ sl@0: } sl@0: sl@0: ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, disabled_mask, sl@0: co_list, &head, &tail); sl@0: sl@0: /* sl@0: * We also need cipher aliases for selecting based on the rule_str. sl@0: * There might be two types of entries in the rule_str: 1) names sl@0: * of ciphers themselves 2) aliases for groups of ciphers. sl@0: * For 1) we need the available ciphers and for 2) the cipher sl@0: * groups of cipher_aliases added together in one list (otherwise sl@0: * we would be happy with just the cipher_aliases table). sl@0: */ sl@0: num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER); sl@0: num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1; sl@0: ca_list = sl@0: (SSL_CIPHER **)OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max); sl@0: if (ca_list == NULL) sl@0: { sl@0: OPENSSL_free(co_list); sl@0: SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE); sl@0: return(NULL); /* Failure */ sl@0: } sl@0: ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, disabled_mask, sl@0: head); sl@0: sl@0: /* sl@0: * If the rule_string begins with DEFAULT, apply the default rule sl@0: * before using the (possibly available) additional rules. sl@0: */ sl@0: ok = 1; sl@0: rule_p = rule_str; sl@0: if (strncmp(rule_str,"DEFAULT",7) == 0) sl@0: { sl@0: ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST, sl@0: co_list, &head, &tail, ca_list); sl@0: rule_p += 7; sl@0: if (*rule_p == ':') sl@0: rule_p++; sl@0: } sl@0: sl@0: if (ok && (strlen(rule_p) > 0)) sl@0: ok = ssl_cipher_process_rulestr(rule_p, co_list, &head, &tail, sl@0: ca_list); sl@0: sl@0: OPENSSL_free(ca_list); /* Not needed anymore */ sl@0: sl@0: if (!ok) sl@0: { /* Rule processing failure */ sl@0: OPENSSL_free(co_list); sl@0: return(NULL); sl@0: } sl@0: /* sl@0: * Allocate new "cipherstack" for the result, return with error sl@0: * if we cannot get one. sl@0: */ sl@0: if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) sl@0: { sl@0: OPENSSL_free(co_list); sl@0: return(NULL); sl@0: } sl@0: sl@0: /* sl@0: * The cipher selection for the list is done. The ciphers are added sl@0: * to the resulting precedence to the STACK_OF(SSL_CIPHER). sl@0: */ sl@0: for (curr = head; curr != NULL; curr = curr->next) sl@0: { sl@0: if (curr->active) sl@0: { sl@0: sk_SSL_CIPHER_push(cipherstack, curr->cipher); sl@0: #ifdef CIPHER_DEBUG sl@0: printf("<%s>\n",curr->cipher->name); sl@0: #endif sl@0: } sl@0: } sl@0: OPENSSL_free(co_list); /* Not needed any longer */ sl@0: sl@0: tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack); sl@0: if (tmp_cipher_list == NULL) sl@0: { sl@0: sk_SSL_CIPHER_free(cipherstack); sl@0: return NULL; sl@0: } sl@0: if (*cipher_list != NULL) sl@0: sk_SSL_CIPHER_free(*cipher_list); sl@0: *cipher_list = cipherstack; sl@0: if (*cipher_list_by_id != NULL) sl@0: sk_SSL_CIPHER_free(*cipher_list_by_id); sl@0: *cipher_list_by_id = tmp_cipher_list; sl@0: (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp); sl@0: sl@0: return(cipherstack); sl@0: } sl@0: sl@0: EXPORT_C char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len) sl@0: { sl@0: int is_export,pkl,kl; sl@0: const char *ver,*exp_str; sl@0: const char *kx,*au,*enc,*mac; sl@0: unsigned long alg,alg2,alg_s; sl@0: #ifndef EMULATOR sl@0: #ifdef KSSL_DEBUG sl@0: static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s AL=%lx\n"; sl@0: #else sl@0: static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s\n"; sl@0: #endif /* KSSL_DEBUG */ sl@0: #else /* EMULATOR */ sl@0: #ifdef KSSL_DEBUG sl@0: static const char *const format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s AL=%lx\n"; sl@0: #else sl@0: static const char *const format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s\n"; sl@0: #endif /* KSSL_DEBUG */ sl@0: sl@0: #endif /* EMULATOR */ sl@0: alg=cipher->algorithms; sl@0: alg_s=cipher->algo_strength; sl@0: alg2=cipher->algorithm2; sl@0: sl@0: is_export=SSL_C_IS_EXPORT(cipher); sl@0: pkl=SSL_C_EXPORT_PKEYLENGTH(cipher); sl@0: kl=SSL_C_EXPORT_KEYLENGTH(cipher); sl@0: exp_str=is_export?" export":""; sl@0: sl@0: if (alg & SSL_SSLV2) sl@0: ver="SSLv2"; sl@0: else if (alg & SSL_SSLV3) sl@0: ver="SSLv3"; sl@0: else sl@0: ver="unknown"; sl@0: sl@0: switch (alg&SSL_MKEY_MASK) sl@0: { sl@0: case SSL_kRSA: sl@0: kx=is_export?(pkl == 512 ? "RSA(512)" : "RSA(1024)"):"RSA"; sl@0: break; sl@0: case SSL_kDHr: sl@0: kx="DH/RSA"; sl@0: break; sl@0: case SSL_kDHd: sl@0: kx="DH/DSS"; sl@0: break; sl@0: case SSL_kKRB5: /* VRS */ sl@0: case SSL_KRB5: /* VRS */ sl@0: kx="KRB5"; sl@0: break; sl@0: case SSL_kFZA: sl@0: kx="Fortezza"; sl@0: break; sl@0: case SSL_kEDH: sl@0: kx=is_export?(pkl == 512 ? "DH(512)" : "DH(1024)"):"DH"; sl@0: break; sl@0: case SSL_kECDH: sl@0: case SSL_kECDHE: sl@0: kx=is_export?"ECDH(<=163)":"ECDH"; sl@0: break; sl@0: default: sl@0: kx="unknown"; sl@0: } sl@0: sl@0: switch (alg&SSL_AUTH_MASK) sl@0: { sl@0: case SSL_aRSA: sl@0: au="RSA"; sl@0: break; sl@0: case SSL_aDSS: sl@0: au="DSS"; sl@0: break; sl@0: case SSL_aDH: sl@0: au="DH"; sl@0: break; sl@0: case SSL_aKRB5: /* VRS */ sl@0: case SSL_KRB5: /* VRS */ sl@0: au="KRB5"; sl@0: break; sl@0: case SSL_aFZA: sl@0: case SSL_aNULL: sl@0: au="None"; sl@0: break; sl@0: case SSL_aECDSA: sl@0: au="ECDSA"; sl@0: break; sl@0: default: sl@0: au="unknown"; sl@0: break; sl@0: } sl@0: sl@0: switch (alg&SSL_ENC_MASK) sl@0: { sl@0: case SSL_DES: sl@0: enc=(is_export && kl == 5)?"DES(40)":"DES(56)"; sl@0: break; sl@0: case SSL_3DES: sl@0: enc="3DES(168)"; sl@0: break; sl@0: case SSL_RC4: sl@0: enc=is_export?(kl == 5 ? "RC4(40)" : "RC4(56)") sl@0: :((alg2&SSL2_CF_8_BYTE_ENC)?"RC4(64)":"RC4(128)"); sl@0: break; sl@0: case SSL_RC2: sl@0: enc=is_export?(kl == 5 ? "RC2(40)" : "RC2(56)"):"RC2(128)"; sl@0: break; sl@0: case SSL_IDEA: sl@0: enc="IDEA(128)"; sl@0: break; sl@0: case SSL_eFZA: sl@0: enc="Fortezza"; sl@0: break; sl@0: case SSL_eNULL: sl@0: enc="None"; sl@0: break; sl@0: case SSL_AES: sl@0: switch(cipher->strength_bits) sl@0: { sl@0: case 128: enc="AES(128)"; break; sl@0: case 192: enc="AES(192)"; break; sl@0: case 256: enc="AES(256)"; break; sl@0: default: enc="AES(?""?""?)"; break; sl@0: } sl@0: break; sl@0: default: sl@0: enc="unknown"; sl@0: break; sl@0: } sl@0: sl@0: switch (alg&SSL_MAC_MASK) sl@0: { sl@0: case SSL_MD5: sl@0: mac="MD5"; sl@0: break; sl@0: case SSL_SHA1: sl@0: mac="SHA1"; sl@0: break; sl@0: default: sl@0: mac="unknown"; sl@0: break; sl@0: } sl@0: sl@0: if (buf == NULL) sl@0: { sl@0: len=128; sl@0: buf=OPENSSL_malloc(len); sl@0: if (buf == NULL) return("OPENSSL_malloc Error"); sl@0: } sl@0: else if (len < 128) sl@0: return("Buffer too small"); sl@0: sl@0: #ifdef KSSL_DEBUG sl@0: BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str,alg); sl@0: #else sl@0: BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str); sl@0: #endif /* KSSL_DEBUG */ sl@0: return(buf); sl@0: } sl@0: sl@0: EXPORT_C char *SSL_CIPHER_get_version(const SSL_CIPHER *c) sl@0: { sl@0: int i; sl@0: sl@0: if (c == NULL) return("(NONE)"); sl@0: i=(int)(c->id>>24L); sl@0: if (i == 3) sl@0: return("TLSv1/SSLv3"); sl@0: else if (i == 2) sl@0: return("SSLv2"); sl@0: else sl@0: return("unknown"); sl@0: } sl@0: sl@0: /* return the actual cipher being used */ sl@0: EXPORT_C const char *SSL_CIPHER_get_name(const SSL_CIPHER *c) sl@0: { sl@0: if (c != NULL) sl@0: return(c->name); sl@0: return("(NONE)"); sl@0: } sl@0: sl@0: /* number of bits for symmetric cipher */ sl@0: EXPORT_C int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits) sl@0: { sl@0: int ret=0; sl@0: sl@0: if (c != NULL) sl@0: { sl@0: if (alg_bits != NULL) *alg_bits = c->alg_bits; sl@0: ret = c->strength_bits; sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n) sl@0: { sl@0: SSL_COMP *ctmp; sl@0: int i,nn; sl@0: sl@0: if ((n == 0) || (sk == NULL)) return(NULL); sl@0: nn=sk_SSL_COMP_num(sk); sl@0: for (i=0; iid == n) sl@0: return(ctmp); sl@0: } sl@0: return(NULL); sl@0: } sl@0: sl@0: #ifdef OPENSSL_NO_COMP sl@0: EXPORT_C void *SSL_COMP_get_compression_methods(void) sl@0: { sl@0: return NULL; sl@0: } sl@0: EXPORT_C int SSL_COMP_add_compression_method(int id, void *cm) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C const char *SSL_COMP_get_name(const void *comp) sl@0: { sl@0: return NULL; sl@0: } sl@0: #else sl@0: EXPORT_C STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void) sl@0: { sl@0: load_builtin_compressions(); sl@0: return(ssl_comp_methods); sl@0: } sl@0: sl@0: EXPORT_C int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) sl@0: { sl@0: SSL_COMP *comp; sl@0: sl@0: if (cm == NULL || cm->type == NID_undef) sl@0: return 1; sl@0: sl@0: /* According to draft-ietf-tls-compression-04.txt, the sl@0: compression number ranges should be the following: sl@0: sl@0: 0 to 63: methods defined by the IETF sl@0: 64 to 192: external party methods assigned by IANA sl@0: 193 to 255: reserved for private use */ sl@0: if (id < 193 || id > 255) sl@0: { sl@0: SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,SSL_R_COMPRESSION_ID_NOT_WITHIN_PRIVATE_RANGE); sl@0: return 0; sl@0: } sl@0: sl@0: MemCheck_off(); sl@0: comp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP)); sl@0: comp->id=id; sl@0: comp->method=cm; sl@0: load_builtin_compressions(); sl@0: if (ssl_comp_methods sl@0: && !sk_SSL_COMP_find(ssl_comp_methods,comp)) sl@0: { sl@0: OPENSSL_free(comp); sl@0: MemCheck_on(); sl@0: SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,SSL_R_DUPLICATE_COMPRESSION_ID); sl@0: return(1); sl@0: } sl@0: else if ((ssl_comp_methods == NULL) sl@0: || !sk_SSL_COMP_push(ssl_comp_methods,comp)) sl@0: { sl@0: OPENSSL_free(comp); sl@0: MemCheck_on(); sl@0: SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,ERR_R_MALLOC_FAILURE); sl@0: return(1); sl@0: } sl@0: else sl@0: { sl@0: MemCheck_on(); sl@0: return(0); sl@0: } sl@0: } sl@0: sl@0: EXPORT_C const char *SSL_COMP_get_name(const COMP_METHOD *comp) sl@0: { sl@0: if (comp) sl@0: return comp->name; sl@0: return NULL; sl@0: } sl@0: sl@0: #endif