10Sstevel@tonic-gate /* ssl/ssl_ciph.c */ 20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 30Sstevel@tonic-gate * All rights reserved. 40Sstevel@tonic-gate * 50Sstevel@tonic-gate * This package is an SSL implementation written 60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com). 70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as 100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions 110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA, 120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation 130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms 140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com). 150Sstevel@tonic-gate * 160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in 170Sstevel@tonic-gate * the code are not to be removed. 180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution 190Sstevel@tonic-gate * as the author of the parts of the library used. 200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or 210Sstevel@tonic-gate * in documentation (online or textual) provided with the package. 220Sstevel@tonic-gate * 230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 240Sstevel@tonic-gate * modification, are permitted provided that the following conditions 250Sstevel@tonic-gate * are met: 260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright 270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 320Sstevel@tonic-gate * must display the following acknowledgement: 330Sstevel@tonic-gate * "This product includes cryptographic software written by 340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)" 350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library 360Sstevel@tonic-gate * being used are not cryptographic related :-). 370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from 380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement: 390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 400Sstevel@tonic-gate * 410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 510Sstevel@tonic-gate * SUCH DAMAGE. 520Sstevel@tonic-gate * 530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or 540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be 550Sstevel@tonic-gate * copied and put under another distribution licence 560Sstevel@tonic-gate * [including the GNU Public Licence.] 570Sstevel@tonic-gate */ 58*2139Sjp161948 /* ==================================================================== 59*2139Sjp161948 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 60*2139Sjp161948 * ECC cipher suite support in OpenSSL originally developed by 61*2139Sjp161948 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. 62*2139Sjp161948 */ 630Sstevel@tonic-gate 640Sstevel@tonic-gate /* 65*2139Sjp161948 * The portions of this code that are #ifdef CRYPTO_UNLIMITED are 660Sstevel@tonic-gate * 67*2139Sjp161948 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 680Sstevel@tonic-gate * Use is subject to license terms. 690Sstevel@tonic-gate * 700Sstevel@tonic-gate */ 71*2139Sjp161948 720Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 730Sstevel@tonic-gate 740Sstevel@tonic-gate #include <stdio.h> 750Sstevel@tonic-gate #include <openssl/objects.h> 760Sstevel@tonic-gate #include <openssl/comp.h> 770Sstevel@tonic-gate #include "ssl_locl.h" 780Sstevel@tonic-gate 790Sstevel@tonic-gate #define SSL_ENC_DES_IDX 0 800Sstevel@tonic-gate #define SSL_ENC_3DES_IDX 1 810Sstevel@tonic-gate #define SSL_ENC_RC4_IDX 2 820Sstevel@tonic-gate #define SSL_ENC_RC2_IDX 3 830Sstevel@tonic-gate #define SSL_ENC_IDEA_IDX 4 840Sstevel@tonic-gate #define SSL_ENC_eFZA_IDX 5 850Sstevel@tonic-gate #define SSL_ENC_NULL_IDX 6 860Sstevel@tonic-gate #define SSL_ENC_AES128_IDX 7 870Sstevel@tonic-gate #define SSL_ENC_AES256_IDX 8 880Sstevel@tonic-gate #define SSL_ENC_NUM_IDX 9 890Sstevel@tonic-gate 900Sstevel@tonic-gate static const EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX]={ 910Sstevel@tonic-gate NULL,NULL,NULL,NULL,NULL,NULL, 920Sstevel@tonic-gate }; 930Sstevel@tonic-gate 94*2139Sjp161948 #define SSL_COMP_NULL_IDX 0 95*2139Sjp161948 #define SSL_COMP_ZLIB_IDX 1 96*2139Sjp161948 #define SSL_COMP_NUM_IDX 2 97*2139Sjp161948 980Sstevel@tonic-gate static STACK_OF(SSL_COMP) *ssl_comp_methods=NULL; 990Sstevel@tonic-gate 1000Sstevel@tonic-gate #define SSL_MD_MD5_IDX 0 1010Sstevel@tonic-gate #define SSL_MD_SHA1_IDX 1 1020Sstevel@tonic-gate #define SSL_MD_NUM_IDX 2 1030Sstevel@tonic-gate static const EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX]={ 1040Sstevel@tonic-gate NULL,NULL, 1050Sstevel@tonic-gate }; 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate #define CIPHER_ADD 1 1080Sstevel@tonic-gate #define CIPHER_KILL 2 1090Sstevel@tonic-gate #define CIPHER_DEL 3 1100Sstevel@tonic-gate #define CIPHER_ORD 4 1110Sstevel@tonic-gate #define CIPHER_SPECIAL 5 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate typedef struct cipher_order_st 1140Sstevel@tonic-gate { 1150Sstevel@tonic-gate SSL_CIPHER *cipher; 1160Sstevel@tonic-gate int active; 1170Sstevel@tonic-gate int dead; 1180Sstevel@tonic-gate struct cipher_order_st *next,*prev; 1190Sstevel@tonic-gate } CIPHER_ORDER; 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate static const SSL_CIPHER cipher_aliases[]={ 1220Sstevel@tonic-gate /* Don't include eNULL unless specifically enabled. */ 123*2139Sjp161948 /* Don't include ECC in ALL because these ciphers are not yet official. */ 124*2139Sjp161948 {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 */ 125*2139Sjp161948 /* TODO: COMPLEMENT OF ALL and COMPLEMENT OF DEFAULT do not have ECC cipher suites handled properly. */ 126*2139Sjp161948 {0,SSL_TXT_CMPALL,0,SSL_eNULL,0,0,0,0,SSL_ENC_MASK,0}, /* COMPLEMENT OF ALL */ 1270Sstevel@tonic-gate {0,SSL_TXT_CMPDEF,0,SSL_ADH, 0,0,0,0,SSL_AUTH_MASK,0}, 128*2139Sjp161948 {0,SSL_TXT_kKRB5,0,SSL_kKRB5,0,0,0,0,SSL_MKEY_MASK,0}, /* VRS Kerberos5 */ 1290Sstevel@tonic-gate {0,SSL_TXT_kRSA,0,SSL_kRSA, 0,0,0,0,SSL_MKEY_MASK,0}, 1300Sstevel@tonic-gate {0,SSL_TXT_kDHr,0,SSL_kDHr, 0,0,0,0,SSL_MKEY_MASK,0}, 1310Sstevel@tonic-gate {0,SSL_TXT_kDHd,0,SSL_kDHd, 0,0,0,0,SSL_MKEY_MASK,0}, 1320Sstevel@tonic-gate {0,SSL_TXT_kEDH,0,SSL_kEDH, 0,0,0,0,SSL_MKEY_MASK,0}, 1330Sstevel@tonic-gate {0,SSL_TXT_kFZA,0,SSL_kFZA, 0,0,0,0,SSL_MKEY_MASK,0}, 1340Sstevel@tonic-gate {0,SSL_TXT_DH, 0,SSL_DH, 0,0,0,0,SSL_MKEY_MASK,0}, 135*2139Sjp161948 {0,SSL_TXT_ECC, 0,(SSL_kECDH|SSL_kECDHE), 0,0,0,0,SSL_MKEY_MASK,0}, 1360Sstevel@tonic-gate {0,SSL_TXT_EDH, 0,SSL_EDH, 0,0,0,0,SSL_MKEY_MASK|SSL_AUTH_MASK,0}, 1370Sstevel@tonic-gate {0,SSL_TXT_aKRB5,0,SSL_aKRB5,0,0,0,0,SSL_AUTH_MASK,0}, /* VRS Kerberos5 */ 1380Sstevel@tonic-gate {0,SSL_TXT_aRSA,0,SSL_aRSA, 0,0,0,0,SSL_AUTH_MASK,0}, 1390Sstevel@tonic-gate {0,SSL_TXT_aDSS,0,SSL_aDSS, 0,0,0,0,SSL_AUTH_MASK,0}, 1400Sstevel@tonic-gate {0,SSL_TXT_aFZA,0,SSL_aFZA, 0,0,0,0,SSL_AUTH_MASK,0}, 1410Sstevel@tonic-gate {0,SSL_TXT_aNULL,0,SSL_aNULL,0,0,0,0,SSL_AUTH_MASK,0}, 1420Sstevel@tonic-gate {0,SSL_TXT_aDH, 0,SSL_aDH, 0,0,0,0,SSL_AUTH_MASK,0}, 1430Sstevel@tonic-gate {0,SSL_TXT_DSS, 0,SSL_DSS, 0,0,0,0,SSL_AUTH_MASK,0}, 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate {0,SSL_TXT_DES, 0,SSL_DES, 0,0,0,0,SSL_ENC_MASK,0}, 1460Sstevel@tonic-gate {0,SSL_TXT_3DES,0,SSL_3DES, 0,0,0,0,SSL_ENC_MASK,0}, 1470Sstevel@tonic-gate {0,SSL_TXT_RC4, 0,SSL_RC4, 0,0,0,0,SSL_ENC_MASK,0}, 1480Sstevel@tonic-gate {0,SSL_TXT_RC2, 0,SSL_RC2, 0,0,0,0,SSL_ENC_MASK,0}, 1490Sstevel@tonic-gate #ifndef OPENSSL_NO_IDEA 1500Sstevel@tonic-gate {0,SSL_TXT_IDEA,0,SSL_IDEA, 0,0,0,0,SSL_ENC_MASK,0}, 1510Sstevel@tonic-gate #endif 1520Sstevel@tonic-gate {0,SSL_TXT_eNULL,0,SSL_eNULL,0,0,0,0,SSL_ENC_MASK,0}, 1530Sstevel@tonic-gate {0,SSL_TXT_eFZA,0,SSL_eFZA, 0,0,0,0,SSL_ENC_MASK,0}, 1540Sstevel@tonic-gate {0,SSL_TXT_AES, 0,SSL_AES, 0,0,0,0,SSL_ENC_MASK,0}, 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate {0,SSL_TXT_MD5, 0,SSL_MD5, 0,0,0,0,SSL_MAC_MASK,0}, 1570Sstevel@tonic-gate {0,SSL_TXT_SHA1,0,SSL_SHA1, 0,0,0,0,SSL_MAC_MASK,0}, 1580Sstevel@tonic-gate {0,SSL_TXT_SHA, 0,SSL_SHA, 0,0,0,0,SSL_MAC_MASK,0}, 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate {0,SSL_TXT_NULL,0,SSL_NULL, 0,0,0,0,SSL_ENC_MASK,0}, 1610Sstevel@tonic-gate {0,SSL_TXT_KRB5,0,SSL_KRB5, 0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK,0}, 1620Sstevel@tonic-gate {0,SSL_TXT_RSA, 0,SSL_RSA, 0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK,0}, 1630Sstevel@tonic-gate {0,SSL_TXT_ADH, 0,SSL_ADH, 0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK,0}, 1640Sstevel@tonic-gate {0,SSL_TXT_FZA, 0,SSL_FZA, 0,0,0,0,SSL_AUTH_MASK|SSL_MKEY_MASK|SSL_ENC_MASK,0}, 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate {0,SSL_TXT_SSLV2, 0,SSL_SSLV2, 0,0,0,0,SSL_SSL_MASK,0}, 1670Sstevel@tonic-gate {0,SSL_TXT_SSLV3, 0,SSL_SSLV3, 0,0,0,0,SSL_SSL_MASK,0}, 1680Sstevel@tonic-gate {0,SSL_TXT_TLSV1, 0,SSL_TLSV1, 0,0,0,0,SSL_SSL_MASK,0}, 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate {0,SSL_TXT_EXP ,0, 0,SSL_EXPORT, 0,0,0,0,SSL_EXP_MASK}, 1710Sstevel@tonic-gate {0,SSL_TXT_EXPORT,0, 0,SSL_EXPORT, 0,0,0,0,SSL_EXP_MASK}, 1720Sstevel@tonic-gate {0,SSL_TXT_EXP40, 0, 0, SSL_EXP40, 0,0,0,0,SSL_STRONG_MASK}, 1730Sstevel@tonic-gate {0,SSL_TXT_EXP56, 0, 0, SSL_EXP56, 0,0,0,0,SSL_STRONG_MASK}, 1740Sstevel@tonic-gate {0,SSL_TXT_LOW, 0, 0, SSL_LOW, 0,0,0,0,SSL_STRONG_MASK}, 1750Sstevel@tonic-gate {0,SSL_TXT_MEDIUM,0, 0,SSL_MEDIUM, 0,0,0,0,SSL_STRONG_MASK}, 1760Sstevel@tonic-gate {0,SSL_TXT_HIGH, 0, 0, SSL_HIGH, 0,0,0,0,SSL_STRONG_MASK}, 1770Sstevel@tonic-gate }; 1780Sstevel@tonic-gate 179*2139Sjp161948 void ssl_load_ciphers(void) 1800Sstevel@tonic-gate { 1810Sstevel@tonic-gate ssl_cipher_methods[SSL_ENC_DES_IDX]= 1820Sstevel@tonic-gate EVP_get_cipherbyname(SN_des_cbc); 1830Sstevel@tonic-gate ssl_cipher_methods[SSL_ENC_3DES_IDX]= 1840Sstevel@tonic-gate EVP_get_cipherbyname(SN_des_ede3_cbc); 1850Sstevel@tonic-gate ssl_cipher_methods[SSL_ENC_RC4_IDX]= 1860Sstevel@tonic-gate EVP_get_cipherbyname(SN_rc4); 1870Sstevel@tonic-gate ssl_cipher_methods[SSL_ENC_RC2_IDX]= 1880Sstevel@tonic-gate EVP_get_cipherbyname(SN_rc2_cbc); 1890Sstevel@tonic-gate #ifndef OPENSSL_NO_IDEA 1900Sstevel@tonic-gate ssl_cipher_methods[SSL_ENC_IDEA_IDX]= 1910Sstevel@tonic-gate EVP_get_cipherbyname(SN_idea_cbc); 1920Sstevel@tonic-gate #else 1930Sstevel@tonic-gate ssl_cipher_methods[SSL_ENC_IDEA_IDX]= NULL; 1940Sstevel@tonic-gate #endif 1950Sstevel@tonic-gate ssl_cipher_methods[SSL_ENC_AES128_IDX]= 1960Sstevel@tonic-gate EVP_get_cipherbyname(SN_aes_128_cbc); 1970Sstevel@tonic-gate #ifdef CRYPTO_UNLIMITED 1980Sstevel@tonic-gate ssl_cipher_methods[SSL_ENC_AES256_IDX]= 1990Sstevel@tonic-gate EVP_get_cipherbyname(SN_aes_256_cbc); 2000Sstevel@tonic-gate #endif 2010Sstevel@tonic-gate ssl_digest_methods[SSL_MD_MD5_IDX]= 2020Sstevel@tonic-gate EVP_get_digestbyname(SN_md5); 2030Sstevel@tonic-gate ssl_digest_methods[SSL_MD_SHA1_IDX]= 2040Sstevel@tonic-gate EVP_get_digestbyname(SN_sha1); 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate 207*2139Sjp161948 208*2139Sjp161948 #ifndef OPENSSL_NO_COMP 209*2139Sjp161948 210*2139Sjp161948 static int sk_comp_cmp(const SSL_COMP * const *a, 211*2139Sjp161948 const SSL_COMP * const *b) 212*2139Sjp161948 { 213*2139Sjp161948 return((*a)->id-(*b)->id); 214*2139Sjp161948 } 215*2139Sjp161948 216*2139Sjp161948 static void load_builtin_compressions(void) 217*2139Sjp161948 { 218*2139Sjp161948 if (ssl_comp_methods != NULL) 219*2139Sjp161948 return; 220*2139Sjp161948 221*2139Sjp161948 CRYPTO_w_lock(CRYPTO_LOCK_SSL); 222*2139Sjp161948 if (ssl_comp_methods == NULL) 223*2139Sjp161948 { 224*2139Sjp161948 SSL_COMP *comp = NULL; 225*2139Sjp161948 226*2139Sjp161948 MemCheck_off(); 227*2139Sjp161948 ssl_comp_methods=sk_SSL_COMP_new(sk_comp_cmp); 228*2139Sjp161948 if (ssl_comp_methods != NULL) 229*2139Sjp161948 { 230*2139Sjp161948 comp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP)); 231*2139Sjp161948 if (comp != NULL) 232*2139Sjp161948 { 233*2139Sjp161948 comp->method=COMP_zlib(); 234*2139Sjp161948 if (comp->method 235*2139Sjp161948 && comp->method->type == NID_undef) 236*2139Sjp161948 OPENSSL_free(comp); 237*2139Sjp161948 else 238*2139Sjp161948 { 239*2139Sjp161948 comp->id=SSL_COMP_ZLIB_IDX; 240*2139Sjp161948 comp->name=comp->method->name; 241*2139Sjp161948 sk_SSL_COMP_push(ssl_comp_methods,comp); 242*2139Sjp161948 } 243*2139Sjp161948 } 244*2139Sjp161948 } 245*2139Sjp161948 MemCheck_on(); 246*2139Sjp161948 } 247*2139Sjp161948 CRYPTO_w_unlock(CRYPTO_LOCK_SSL); 248*2139Sjp161948 } 249*2139Sjp161948 #endif 250*2139Sjp161948 251*2139Sjp161948 int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc, 2520Sstevel@tonic-gate const EVP_MD **md, SSL_COMP **comp) 2530Sstevel@tonic-gate { 2540Sstevel@tonic-gate int i; 2550Sstevel@tonic-gate SSL_CIPHER *c; 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate c=s->cipher; 2580Sstevel@tonic-gate if (c == NULL) return(0); 2590Sstevel@tonic-gate if (comp != NULL) 2600Sstevel@tonic-gate { 2610Sstevel@tonic-gate SSL_COMP ctmp; 262*2139Sjp161948 #ifndef OPENSSL_NO_COMP 263*2139Sjp161948 load_builtin_compressions(); 264*2139Sjp161948 #endif 2650Sstevel@tonic-gate 266*2139Sjp161948 *comp=NULL; 267*2139Sjp161948 ctmp.id=s->compress_meth; 268*2139Sjp161948 if (ssl_comp_methods != NULL) 2690Sstevel@tonic-gate { 2700Sstevel@tonic-gate i=sk_SSL_COMP_find(ssl_comp_methods,&ctmp); 2710Sstevel@tonic-gate if (i >= 0) 2720Sstevel@tonic-gate *comp=sk_SSL_COMP_value(ssl_comp_methods,i); 2730Sstevel@tonic-gate else 2740Sstevel@tonic-gate *comp=NULL; 2750Sstevel@tonic-gate } 2760Sstevel@tonic-gate } 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate if ((enc == NULL) || (md == NULL)) return(0); 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate switch (c->algorithms & SSL_ENC_MASK) 2810Sstevel@tonic-gate { 2820Sstevel@tonic-gate case SSL_DES: 2830Sstevel@tonic-gate i=SSL_ENC_DES_IDX; 2840Sstevel@tonic-gate break; 2850Sstevel@tonic-gate case SSL_3DES: 2860Sstevel@tonic-gate i=SSL_ENC_3DES_IDX; 2870Sstevel@tonic-gate break; 2880Sstevel@tonic-gate case SSL_RC4: 2890Sstevel@tonic-gate i=SSL_ENC_RC4_IDX; 2900Sstevel@tonic-gate break; 2910Sstevel@tonic-gate case SSL_RC2: 2920Sstevel@tonic-gate i=SSL_ENC_RC2_IDX; 2930Sstevel@tonic-gate break; 2940Sstevel@tonic-gate case SSL_IDEA: 2950Sstevel@tonic-gate i=SSL_ENC_IDEA_IDX; 2960Sstevel@tonic-gate break; 2970Sstevel@tonic-gate case SSL_eNULL: 2980Sstevel@tonic-gate i=SSL_ENC_NULL_IDX; 2990Sstevel@tonic-gate break; 3000Sstevel@tonic-gate case SSL_AES: 3010Sstevel@tonic-gate switch(c->alg_bits) 3020Sstevel@tonic-gate { 3030Sstevel@tonic-gate case 128: i=SSL_ENC_AES128_IDX; break; 3040Sstevel@tonic-gate case 256: i=SSL_ENC_AES256_IDX; break; 3050Sstevel@tonic-gate default: i=-1; break; 3060Sstevel@tonic-gate } 3070Sstevel@tonic-gate break; 3080Sstevel@tonic-gate default: 3090Sstevel@tonic-gate i= -1; 3100Sstevel@tonic-gate break; 3110Sstevel@tonic-gate } 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate if ((i < 0) || (i > SSL_ENC_NUM_IDX)) 3140Sstevel@tonic-gate *enc=NULL; 3150Sstevel@tonic-gate else 3160Sstevel@tonic-gate { 3170Sstevel@tonic-gate if (i == SSL_ENC_NULL_IDX) 3180Sstevel@tonic-gate *enc=EVP_enc_null(); 3190Sstevel@tonic-gate else 3200Sstevel@tonic-gate *enc=ssl_cipher_methods[i]; 3210Sstevel@tonic-gate } 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate switch (c->algorithms & SSL_MAC_MASK) 3240Sstevel@tonic-gate { 3250Sstevel@tonic-gate case SSL_MD5: 3260Sstevel@tonic-gate i=SSL_MD_MD5_IDX; 3270Sstevel@tonic-gate break; 3280Sstevel@tonic-gate case SSL_SHA1: 3290Sstevel@tonic-gate i=SSL_MD_SHA1_IDX; 3300Sstevel@tonic-gate break; 3310Sstevel@tonic-gate default: 3320Sstevel@tonic-gate i= -1; 3330Sstevel@tonic-gate break; 3340Sstevel@tonic-gate } 3350Sstevel@tonic-gate if ((i < 0) || (i > SSL_MD_NUM_IDX)) 3360Sstevel@tonic-gate *md=NULL; 3370Sstevel@tonic-gate else 3380Sstevel@tonic-gate *md=ssl_digest_methods[i]; 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate if ((*enc != NULL) && (*md != NULL)) 3410Sstevel@tonic-gate return(1); 3420Sstevel@tonic-gate else 3430Sstevel@tonic-gate return(0); 3440Sstevel@tonic-gate } 3450Sstevel@tonic-gate 3460Sstevel@tonic-gate #define ITEM_SEP(a) \ 3470Sstevel@tonic-gate (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ',')) 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr, 3500Sstevel@tonic-gate CIPHER_ORDER **tail) 3510Sstevel@tonic-gate { 3520Sstevel@tonic-gate if (curr == *tail) return; 3530Sstevel@tonic-gate if (curr == *head) 3540Sstevel@tonic-gate *head=curr->next; 3550Sstevel@tonic-gate if (curr->prev != NULL) 3560Sstevel@tonic-gate curr->prev->next=curr->next; 3570Sstevel@tonic-gate if (curr->next != NULL) /* should always be true */ 3580Sstevel@tonic-gate curr->next->prev=curr->prev; 3590Sstevel@tonic-gate (*tail)->next=curr; 3600Sstevel@tonic-gate curr->prev= *tail; 3610Sstevel@tonic-gate curr->next=NULL; 3620Sstevel@tonic-gate *tail=curr; 3630Sstevel@tonic-gate } 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate static unsigned long ssl_cipher_get_disabled(void) 3660Sstevel@tonic-gate { 3670Sstevel@tonic-gate unsigned long mask; 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate mask = SSL_kFZA; 3700Sstevel@tonic-gate #ifdef OPENSSL_NO_RSA 3710Sstevel@tonic-gate mask |= SSL_aRSA|SSL_kRSA; 3720Sstevel@tonic-gate #endif 3730Sstevel@tonic-gate #ifdef OPENSSL_NO_DSA 3740Sstevel@tonic-gate mask |= SSL_aDSS; 3750Sstevel@tonic-gate #endif 3760Sstevel@tonic-gate #ifdef OPENSSL_NO_DH 3770Sstevel@tonic-gate mask |= SSL_kDHr|SSL_kDHd|SSL_kEDH|SSL_aDH; 3780Sstevel@tonic-gate #endif 3790Sstevel@tonic-gate #ifdef OPENSSL_NO_KRB5 3800Sstevel@tonic-gate mask |= SSL_kKRB5|SSL_aKRB5; 3810Sstevel@tonic-gate #endif 382*2139Sjp161948 #ifdef OPENSSL_NO_ECDH 383*2139Sjp161948 mask |= SSL_kECDH|SSL_kECDHE; 384*2139Sjp161948 #endif 3850Sstevel@tonic-gate #ifdef SSL_FORBID_ENULL 3860Sstevel@tonic-gate mask |= SSL_eNULL; 3870Sstevel@tonic-gate #endif 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate mask |= (ssl_cipher_methods[SSL_ENC_DES_IDX ] == NULL) ? SSL_DES :0; 3900Sstevel@tonic-gate mask |= (ssl_cipher_methods[SSL_ENC_3DES_IDX] == NULL) ? SSL_3DES:0; 3910Sstevel@tonic-gate mask |= (ssl_cipher_methods[SSL_ENC_RC4_IDX ] == NULL) ? SSL_RC4 :0; 3920Sstevel@tonic-gate mask |= (ssl_cipher_methods[SSL_ENC_RC2_IDX ] == NULL) ? SSL_RC2 :0; 3930Sstevel@tonic-gate mask |= (ssl_cipher_methods[SSL_ENC_IDEA_IDX] == NULL) ? SSL_IDEA:0; 3940Sstevel@tonic-gate mask |= (ssl_cipher_methods[SSL_ENC_eFZA_IDX] == NULL) ? SSL_eFZA:0; 3950Sstevel@tonic-gate mask |= (ssl_cipher_methods[SSL_ENC_AES128_IDX] == NULL) ? SSL_AES:0; 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate mask |= (ssl_digest_methods[SSL_MD_MD5_IDX ] == NULL) ? SSL_MD5 :0; 3980Sstevel@tonic-gate mask |= (ssl_digest_methods[SSL_MD_SHA1_IDX] == NULL) ? SSL_SHA1:0; 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate return(mask); 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method, 4040Sstevel@tonic-gate int num_of_ciphers, unsigned long mask, CIPHER_ORDER *co_list, 4050Sstevel@tonic-gate CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) 4060Sstevel@tonic-gate { 4070Sstevel@tonic-gate int i, co_list_num; 4080Sstevel@tonic-gate SSL_CIPHER *c; 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate /* 4110Sstevel@tonic-gate * We have num_of_ciphers descriptions compiled in, depending on the 4120Sstevel@tonic-gate * method selected (SSLv2 and/or SSLv3, TLSv1 etc). 4130Sstevel@tonic-gate * These will later be sorted in a linked list with at most num 4140Sstevel@tonic-gate * entries. 4150Sstevel@tonic-gate */ 4160Sstevel@tonic-gate 4170Sstevel@tonic-gate /* Get the initial list of ciphers */ 4180Sstevel@tonic-gate co_list_num = 0; /* actual count of ciphers */ 4190Sstevel@tonic-gate for (i = 0; i < num_of_ciphers; i++) 4200Sstevel@tonic-gate { 4210Sstevel@tonic-gate c = ssl_method->get_cipher(i); 4220Sstevel@tonic-gate /* drop those that use any of that is not available */ 4230Sstevel@tonic-gate if ((c != NULL) && c->valid && !(c->algorithms & mask)) 4240Sstevel@tonic-gate { 4250Sstevel@tonic-gate co_list[co_list_num].cipher = c; 4260Sstevel@tonic-gate co_list[co_list_num].next = NULL; 4270Sstevel@tonic-gate co_list[co_list_num].prev = NULL; 4280Sstevel@tonic-gate co_list[co_list_num].active = 0; 4290Sstevel@tonic-gate co_list_num++; 4300Sstevel@tonic-gate #ifdef KSSL_DEBUG 4310Sstevel@tonic-gate printf("\t%d: %s %lx %lx\n",i,c->name,c->id,c->algorithms); 4320Sstevel@tonic-gate #endif /* KSSL_DEBUG */ 4330Sstevel@tonic-gate /* 4340Sstevel@tonic-gate if (!sk_push(ca_list,(char *)c)) goto err; 4350Sstevel@tonic-gate */ 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate } 4380Sstevel@tonic-gate 4390Sstevel@tonic-gate /* 4400Sstevel@tonic-gate * Prepare linked list from list entries 4410Sstevel@tonic-gate */ 4420Sstevel@tonic-gate for (i = 1; i < co_list_num - 1; i++) 4430Sstevel@tonic-gate { 4440Sstevel@tonic-gate co_list[i].prev = &(co_list[i-1]); 4450Sstevel@tonic-gate co_list[i].next = &(co_list[i+1]); 4460Sstevel@tonic-gate } 4470Sstevel@tonic-gate if (co_list_num > 0) 4480Sstevel@tonic-gate { 4490Sstevel@tonic-gate (*head_p) = &(co_list[0]); 4500Sstevel@tonic-gate (*head_p)->prev = NULL; 4510Sstevel@tonic-gate (*head_p)->next = &(co_list[1]); 4520Sstevel@tonic-gate (*tail_p) = &(co_list[co_list_num - 1]); 4530Sstevel@tonic-gate (*tail_p)->prev = &(co_list[co_list_num - 2]); 4540Sstevel@tonic-gate (*tail_p)->next = NULL; 4550Sstevel@tonic-gate } 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate static void ssl_cipher_collect_aliases(SSL_CIPHER **ca_list, 4590Sstevel@tonic-gate int num_of_group_aliases, unsigned long mask, 4600Sstevel@tonic-gate CIPHER_ORDER *head) 4610Sstevel@tonic-gate { 4620Sstevel@tonic-gate CIPHER_ORDER *ciph_curr; 4630Sstevel@tonic-gate SSL_CIPHER **ca_curr; 4640Sstevel@tonic-gate int i; 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate /* 4670Sstevel@tonic-gate * First, add the real ciphers as already collected 4680Sstevel@tonic-gate */ 4690Sstevel@tonic-gate ciph_curr = head; 4700Sstevel@tonic-gate ca_curr = ca_list; 4710Sstevel@tonic-gate while (ciph_curr != NULL) 4720Sstevel@tonic-gate { 4730Sstevel@tonic-gate *ca_curr = ciph_curr->cipher; 4740Sstevel@tonic-gate ca_curr++; 4750Sstevel@tonic-gate ciph_curr = ciph_curr->next; 4760Sstevel@tonic-gate } 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate /* 4790Sstevel@tonic-gate * Now we add the available ones from the cipher_aliases[] table. 4800Sstevel@tonic-gate * They represent either an algorithm, that must be fully 4810Sstevel@tonic-gate * supported (not match any bit in mask) or represent a cipher 4820Sstevel@tonic-gate * strength value (will be added in any case because algorithms=0). 4830Sstevel@tonic-gate */ 4840Sstevel@tonic-gate for (i = 0; i < num_of_group_aliases; i++) 4850Sstevel@tonic-gate { 4860Sstevel@tonic-gate if ((i == 0) || /* always fetch "ALL" */ 4870Sstevel@tonic-gate !(cipher_aliases[i].algorithms & mask)) 4880Sstevel@tonic-gate { 4890Sstevel@tonic-gate *ca_curr = (SSL_CIPHER *)(cipher_aliases + i); 4900Sstevel@tonic-gate ca_curr++; 4910Sstevel@tonic-gate } 4920Sstevel@tonic-gate } 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate *ca_curr = NULL; /* end of list */ 4950Sstevel@tonic-gate } 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate static void ssl_cipher_apply_rule(unsigned long algorithms, unsigned long mask, 4980Sstevel@tonic-gate unsigned long algo_strength, unsigned long mask_strength, 4990Sstevel@tonic-gate int rule, int strength_bits, CIPHER_ORDER *co_list, 5000Sstevel@tonic-gate CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) 5010Sstevel@tonic-gate { 5020Sstevel@tonic-gate CIPHER_ORDER *head, *tail, *curr, *curr2, *tail2; 5030Sstevel@tonic-gate SSL_CIPHER *cp; 5040Sstevel@tonic-gate unsigned long ma, ma_s; 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate #ifdef CIPHER_DEBUG 5070Sstevel@tonic-gate printf("Applying rule %d with %08lx %08lx %08lx %08lx (%d)\n", 5080Sstevel@tonic-gate rule, algorithms, mask, algo_strength, mask_strength, 5090Sstevel@tonic-gate strength_bits); 5100Sstevel@tonic-gate #endif 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate curr = head = *head_p; 5130Sstevel@tonic-gate curr2 = head; 5140Sstevel@tonic-gate tail2 = tail = *tail_p; 5150Sstevel@tonic-gate for (;;) 5160Sstevel@tonic-gate { 5170Sstevel@tonic-gate if ((curr == NULL) || (curr == tail2)) break; 5180Sstevel@tonic-gate curr = curr2; 5190Sstevel@tonic-gate curr2 = curr->next; 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate cp = curr->cipher; 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate /* 5240Sstevel@tonic-gate * Selection criteria is either the number of strength_bits 5250Sstevel@tonic-gate * or the algorithm used. 5260Sstevel@tonic-gate */ 5270Sstevel@tonic-gate if (strength_bits == -1) 5280Sstevel@tonic-gate { 5290Sstevel@tonic-gate ma = mask & cp->algorithms; 5300Sstevel@tonic-gate ma_s = mask_strength & cp->algo_strength; 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate #ifdef CIPHER_DEBUG 5330Sstevel@tonic-gate printf("\nName: %s:\nAlgo = %08lx Algo_strength = %08lx\nMask = %08lx Mask_strength %08lx\n", cp->name, cp->algorithms, cp->algo_strength, mask, mask_strength); 5340Sstevel@tonic-gate printf("ma = %08lx ma_s %08lx, ma&algo=%08lx, ma_s&algos=%08lx\n", ma, ma_s, ma&algorithms, ma_s&algo_strength); 5350Sstevel@tonic-gate #endif 5360Sstevel@tonic-gate /* 5370Sstevel@tonic-gate * Select: if none of the mask bit was met from the 5380Sstevel@tonic-gate * cipher or not all of the bits were met, the 5390Sstevel@tonic-gate * selection does not apply. 5400Sstevel@tonic-gate */ 5410Sstevel@tonic-gate if (((ma == 0) && (ma_s == 0)) || 5420Sstevel@tonic-gate ((ma & algorithms) != ma) || 5430Sstevel@tonic-gate ((ma_s & algo_strength) != ma_s)) 5440Sstevel@tonic-gate continue; /* does not apply */ 5450Sstevel@tonic-gate } 5460Sstevel@tonic-gate else if (strength_bits != cp->strength_bits) 5470Sstevel@tonic-gate continue; /* does not apply */ 5480Sstevel@tonic-gate 5490Sstevel@tonic-gate #ifdef CIPHER_DEBUG 5500Sstevel@tonic-gate printf("Action = %d\n", rule); 5510Sstevel@tonic-gate #endif 5520Sstevel@tonic-gate 5530Sstevel@tonic-gate /* add the cipher if it has not been added yet. */ 5540Sstevel@tonic-gate if (rule == CIPHER_ADD) 5550Sstevel@tonic-gate { 5560Sstevel@tonic-gate if (!curr->active) 5570Sstevel@tonic-gate { 5580Sstevel@tonic-gate ll_append_tail(&head, curr, &tail); 5590Sstevel@tonic-gate curr->active = 1; 5600Sstevel@tonic-gate } 5610Sstevel@tonic-gate } 5620Sstevel@tonic-gate /* Move the added cipher to this location */ 5630Sstevel@tonic-gate else if (rule == CIPHER_ORD) 5640Sstevel@tonic-gate { 5650Sstevel@tonic-gate if (curr->active) 5660Sstevel@tonic-gate { 5670Sstevel@tonic-gate ll_append_tail(&head, curr, &tail); 5680Sstevel@tonic-gate } 5690Sstevel@tonic-gate } 5700Sstevel@tonic-gate else if (rule == CIPHER_DEL) 5710Sstevel@tonic-gate curr->active = 0; 5720Sstevel@tonic-gate else if (rule == CIPHER_KILL) 5730Sstevel@tonic-gate { 5740Sstevel@tonic-gate if (head == curr) 5750Sstevel@tonic-gate head = curr->next; 5760Sstevel@tonic-gate else 5770Sstevel@tonic-gate curr->prev->next = curr->next; 5780Sstevel@tonic-gate if (tail == curr) 5790Sstevel@tonic-gate tail = curr->prev; 5800Sstevel@tonic-gate curr->active = 0; 5810Sstevel@tonic-gate if (curr->next != NULL) 5820Sstevel@tonic-gate curr->next->prev = curr->prev; 5830Sstevel@tonic-gate if (curr->prev != NULL) 5840Sstevel@tonic-gate curr->prev->next = curr->next; 5850Sstevel@tonic-gate curr->next = NULL; 5860Sstevel@tonic-gate curr->prev = NULL; 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate } 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate *head_p = head; 5910Sstevel@tonic-gate *tail_p = tail; 5920Sstevel@tonic-gate } 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate static int ssl_cipher_strength_sort(CIPHER_ORDER *co_list, 5950Sstevel@tonic-gate CIPHER_ORDER **head_p, 5960Sstevel@tonic-gate CIPHER_ORDER **tail_p) 5970Sstevel@tonic-gate { 5980Sstevel@tonic-gate int max_strength_bits, i, *number_uses; 5990Sstevel@tonic-gate CIPHER_ORDER *curr; 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate /* 6020Sstevel@tonic-gate * This routine sorts the ciphers with descending strength. The sorting 6030Sstevel@tonic-gate * must keep the pre-sorted sequence, so we apply the normal sorting 6040Sstevel@tonic-gate * routine as '+' movement to the end of the list. 6050Sstevel@tonic-gate */ 6060Sstevel@tonic-gate max_strength_bits = 0; 6070Sstevel@tonic-gate curr = *head_p; 6080Sstevel@tonic-gate while (curr != NULL) 6090Sstevel@tonic-gate { 6100Sstevel@tonic-gate if (curr->active && 6110Sstevel@tonic-gate (curr->cipher->strength_bits > max_strength_bits)) 6120Sstevel@tonic-gate max_strength_bits = curr->cipher->strength_bits; 6130Sstevel@tonic-gate curr = curr->next; 6140Sstevel@tonic-gate } 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int)); 6170Sstevel@tonic-gate if (!number_uses) 6180Sstevel@tonic-gate { 6190Sstevel@tonic-gate SSLerr(SSL_F_SSL_CIPHER_STRENGTH_SORT,ERR_R_MALLOC_FAILURE); 6200Sstevel@tonic-gate return(0); 6210Sstevel@tonic-gate } 6220Sstevel@tonic-gate memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int)); 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate /* 6250Sstevel@tonic-gate * Now find the strength_bits values actually used 6260Sstevel@tonic-gate */ 6270Sstevel@tonic-gate curr = *head_p; 6280Sstevel@tonic-gate while (curr != NULL) 6290Sstevel@tonic-gate { 6300Sstevel@tonic-gate if (curr->active) 6310Sstevel@tonic-gate number_uses[curr->cipher->strength_bits]++; 6320Sstevel@tonic-gate curr = curr->next; 6330Sstevel@tonic-gate } 6340Sstevel@tonic-gate /* 6350Sstevel@tonic-gate * Go through the list of used strength_bits values in descending 6360Sstevel@tonic-gate * order. 6370Sstevel@tonic-gate */ 6380Sstevel@tonic-gate for (i = max_strength_bits; i >= 0; i--) 6390Sstevel@tonic-gate if (number_uses[i] > 0) 6400Sstevel@tonic-gate ssl_cipher_apply_rule(0, 0, 0, 0, CIPHER_ORD, i, 6410Sstevel@tonic-gate co_list, head_p, tail_p); 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate OPENSSL_free(number_uses); 6440Sstevel@tonic-gate return(1); 6450Sstevel@tonic-gate } 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate static int ssl_cipher_process_rulestr(const char *rule_str, 6480Sstevel@tonic-gate CIPHER_ORDER *co_list, CIPHER_ORDER **head_p, 6490Sstevel@tonic-gate CIPHER_ORDER **tail_p, SSL_CIPHER **ca_list) 6500Sstevel@tonic-gate { 6510Sstevel@tonic-gate unsigned long algorithms, mask, algo_strength, mask_strength; 6520Sstevel@tonic-gate const char *l, *start, *buf; 6530Sstevel@tonic-gate int j, multi, found, rule, retval, ok, buflen; 6540Sstevel@tonic-gate char ch; 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate retval = 1; 6570Sstevel@tonic-gate l = rule_str; 6580Sstevel@tonic-gate for (;;) 6590Sstevel@tonic-gate { 6600Sstevel@tonic-gate ch = *l; 6610Sstevel@tonic-gate 6620Sstevel@tonic-gate if (ch == '\0') 6630Sstevel@tonic-gate break; /* done */ 6640Sstevel@tonic-gate if (ch == '-') 6650Sstevel@tonic-gate { rule = CIPHER_DEL; l++; } 6660Sstevel@tonic-gate else if (ch == '+') 6670Sstevel@tonic-gate { rule = CIPHER_ORD; l++; } 6680Sstevel@tonic-gate else if (ch == '!') 6690Sstevel@tonic-gate { rule = CIPHER_KILL; l++; } 6700Sstevel@tonic-gate else if (ch == '@') 6710Sstevel@tonic-gate { rule = CIPHER_SPECIAL; l++; } 6720Sstevel@tonic-gate else 6730Sstevel@tonic-gate { rule = CIPHER_ADD; } 6740Sstevel@tonic-gate 6750Sstevel@tonic-gate if (ITEM_SEP(ch)) 6760Sstevel@tonic-gate { 6770Sstevel@tonic-gate l++; 6780Sstevel@tonic-gate continue; 6790Sstevel@tonic-gate } 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate algorithms = mask = algo_strength = mask_strength = 0; 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate start=l; 6840Sstevel@tonic-gate for (;;) 6850Sstevel@tonic-gate { 6860Sstevel@tonic-gate ch = *l; 6870Sstevel@tonic-gate buf = l; 6880Sstevel@tonic-gate buflen = 0; 6890Sstevel@tonic-gate #ifndef CHARSET_EBCDIC 6900Sstevel@tonic-gate while ( ((ch >= 'A') && (ch <= 'Z')) || 6910Sstevel@tonic-gate ((ch >= '0') && (ch <= '9')) || 6920Sstevel@tonic-gate ((ch >= 'a') && (ch <= 'z')) || 6930Sstevel@tonic-gate (ch == '-')) 6940Sstevel@tonic-gate #else 6950Sstevel@tonic-gate while ( isalnum(ch) || (ch == '-')) 6960Sstevel@tonic-gate #endif 6970Sstevel@tonic-gate { 6980Sstevel@tonic-gate ch = *(++l); 6990Sstevel@tonic-gate buflen++; 7000Sstevel@tonic-gate } 7010Sstevel@tonic-gate 7020Sstevel@tonic-gate if (buflen == 0) 7030Sstevel@tonic-gate { 7040Sstevel@tonic-gate /* 7050Sstevel@tonic-gate * We hit something we cannot deal with, 7060Sstevel@tonic-gate * it is no command or separator nor 7070Sstevel@tonic-gate * alphanumeric, so we call this an error. 7080Sstevel@tonic-gate */ 7090Sstevel@tonic-gate SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR, 7100Sstevel@tonic-gate SSL_R_INVALID_COMMAND); 7110Sstevel@tonic-gate retval = found = 0; 7120Sstevel@tonic-gate l++; 7130Sstevel@tonic-gate break; 7140Sstevel@tonic-gate } 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate if (rule == CIPHER_SPECIAL) 7170Sstevel@tonic-gate { 7180Sstevel@tonic-gate found = 0; /* unused -- avoid compiler warning */ 7190Sstevel@tonic-gate break; /* special treatment */ 7200Sstevel@tonic-gate } 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate /* check for multi-part specification */ 7230Sstevel@tonic-gate if (ch == '+') 7240Sstevel@tonic-gate { 7250Sstevel@tonic-gate multi=1; 7260Sstevel@tonic-gate l++; 7270Sstevel@tonic-gate } 7280Sstevel@tonic-gate else 7290Sstevel@tonic-gate multi=0; 7300Sstevel@tonic-gate 7310Sstevel@tonic-gate /* 7320Sstevel@tonic-gate * Now search for the cipher alias in the ca_list. Be careful 7330Sstevel@tonic-gate * with the strncmp, because the "buflen" limitation 7340Sstevel@tonic-gate * will make the rule "ADH:SOME" and the cipher 7350Sstevel@tonic-gate * "ADH-MY-CIPHER" look like a match for buflen=3. 7360Sstevel@tonic-gate * So additionally check whether the cipher name found 7370Sstevel@tonic-gate * has the correct length. We can save a strlen() call: 7380Sstevel@tonic-gate * just checking for the '\0' at the right place is 7390Sstevel@tonic-gate * sufficient, we have to strncmp() anyway. (We cannot 7400Sstevel@tonic-gate * use strcmp(), because buf is not '\0' terminated.) 7410Sstevel@tonic-gate */ 7420Sstevel@tonic-gate j = found = 0; 7430Sstevel@tonic-gate while (ca_list[j]) 7440Sstevel@tonic-gate { 7450Sstevel@tonic-gate if (!strncmp(buf, ca_list[j]->name, buflen) && 7460Sstevel@tonic-gate (ca_list[j]->name[buflen] == '\0')) 7470Sstevel@tonic-gate { 7480Sstevel@tonic-gate found = 1; 7490Sstevel@tonic-gate break; 7500Sstevel@tonic-gate } 7510Sstevel@tonic-gate else 7520Sstevel@tonic-gate j++; 7530Sstevel@tonic-gate } 7540Sstevel@tonic-gate if (!found) 7550Sstevel@tonic-gate break; /* ignore this entry */ 7560Sstevel@tonic-gate 757*2139Sjp161948 /* New algorithms: 758*2139Sjp161948 * 1 - any old restrictions apply outside new mask 759*2139Sjp161948 * 2 - any new restrictions apply outside old mask 760*2139Sjp161948 * 3 - enforce old & new where masks intersect 761*2139Sjp161948 */ 762*2139Sjp161948 algorithms = (algorithms & ~ca_list[j]->mask) | /* 1 */ 763*2139Sjp161948 (ca_list[j]->algorithms & ~mask) | /* 2 */ 764*2139Sjp161948 (algorithms & ca_list[j]->algorithms); /* 3 */ 7650Sstevel@tonic-gate mask |= ca_list[j]->mask; 766*2139Sjp161948 algo_strength = (algo_strength & ~ca_list[j]->mask_strength) | 767*2139Sjp161948 (ca_list[j]->algo_strength & ~mask_strength) | 768*2139Sjp161948 (algo_strength & ca_list[j]->algo_strength); 7690Sstevel@tonic-gate mask_strength |= ca_list[j]->mask_strength; 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate if (!multi) break; 7720Sstevel@tonic-gate } 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate /* 7750Sstevel@tonic-gate * Ok, we have the rule, now apply it 7760Sstevel@tonic-gate */ 7770Sstevel@tonic-gate if (rule == CIPHER_SPECIAL) 7780Sstevel@tonic-gate { /* special command */ 7790Sstevel@tonic-gate ok = 0; 7800Sstevel@tonic-gate if ((buflen == 8) && 7810Sstevel@tonic-gate !strncmp(buf, "STRENGTH", 8)) 7820Sstevel@tonic-gate ok = ssl_cipher_strength_sort(co_list, 7830Sstevel@tonic-gate head_p, tail_p); 7840Sstevel@tonic-gate else 7850Sstevel@tonic-gate SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR, 7860Sstevel@tonic-gate SSL_R_INVALID_COMMAND); 7870Sstevel@tonic-gate if (ok == 0) 7880Sstevel@tonic-gate retval = 0; 7890Sstevel@tonic-gate /* 7900Sstevel@tonic-gate * We do not support any "multi" options 7910Sstevel@tonic-gate * together with "@", so throw away the 7920Sstevel@tonic-gate * rest of the command, if any left, until 7930Sstevel@tonic-gate * end or ':' is found. 7940Sstevel@tonic-gate */ 7950Sstevel@tonic-gate while ((*l != '\0') && ITEM_SEP(*l)) 7960Sstevel@tonic-gate l++; 7970Sstevel@tonic-gate } 7980Sstevel@tonic-gate else if (found) 7990Sstevel@tonic-gate { 8000Sstevel@tonic-gate ssl_cipher_apply_rule(algorithms, mask, 8010Sstevel@tonic-gate algo_strength, mask_strength, rule, -1, 8020Sstevel@tonic-gate co_list, head_p, tail_p); 8030Sstevel@tonic-gate } 8040Sstevel@tonic-gate else 8050Sstevel@tonic-gate { 8060Sstevel@tonic-gate while ((*l != '\0') && ITEM_SEP(*l)) 8070Sstevel@tonic-gate l++; 8080Sstevel@tonic-gate } 8090Sstevel@tonic-gate if (*l == '\0') break; /* done */ 8100Sstevel@tonic-gate } 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate return(retval); 8130Sstevel@tonic-gate } 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method, 8160Sstevel@tonic-gate STACK_OF(SSL_CIPHER) **cipher_list, 8170Sstevel@tonic-gate STACK_OF(SSL_CIPHER) **cipher_list_by_id, 8180Sstevel@tonic-gate const char *rule_str) 8190Sstevel@tonic-gate { 8200Sstevel@tonic-gate int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases; 8210Sstevel@tonic-gate unsigned long disabled_mask; 822*2139Sjp161948 STACK_OF(SSL_CIPHER) *cipherstack, *tmp_cipher_list; 8230Sstevel@tonic-gate const char *rule_p; 8240Sstevel@tonic-gate CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr; 8250Sstevel@tonic-gate SSL_CIPHER **ca_list = NULL; 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate /* 8280Sstevel@tonic-gate * Return with error if nothing to do. 8290Sstevel@tonic-gate */ 830*2139Sjp161948 if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL) 831*2139Sjp161948 return NULL; 8320Sstevel@tonic-gate 8330Sstevel@tonic-gate /* 8340Sstevel@tonic-gate * To reduce the work to do we only want to process the compiled 8350Sstevel@tonic-gate * in algorithms, so we first get the mask of disabled ciphers. 8360Sstevel@tonic-gate */ 8370Sstevel@tonic-gate disabled_mask = ssl_cipher_get_disabled(); 8380Sstevel@tonic-gate 8390Sstevel@tonic-gate /* 8400Sstevel@tonic-gate * Now we have to collect the available ciphers from the compiled 8410Sstevel@tonic-gate * in ciphers. We cannot get more than the number compiled in, so 8420Sstevel@tonic-gate * it is used for allocation. 8430Sstevel@tonic-gate */ 8440Sstevel@tonic-gate num_of_ciphers = ssl_method->num_ciphers(); 8450Sstevel@tonic-gate #ifdef KSSL_DEBUG 8460Sstevel@tonic-gate printf("ssl_create_cipher_list() for %d ciphers\n", num_of_ciphers); 8470Sstevel@tonic-gate #endif /* KSSL_DEBUG */ 8480Sstevel@tonic-gate co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers); 8490Sstevel@tonic-gate if (co_list == NULL) 8500Sstevel@tonic-gate { 8510Sstevel@tonic-gate SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE); 8520Sstevel@tonic-gate return(NULL); /* Failure */ 8530Sstevel@tonic-gate } 8540Sstevel@tonic-gate 8550Sstevel@tonic-gate ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, disabled_mask, 8560Sstevel@tonic-gate co_list, &head, &tail); 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate /* 8590Sstevel@tonic-gate * We also need cipher aliases for selecting based on the rule_str. 8600Sstevel@tonic-gate * There might be two types of entries in the rule_str: 1) names 8610Sstevel@tonic-gate * of ciphers themselves 2) aliases for groups of ciphers. 8620Sstevel@tonic-gate * For 1) we need the available ciphers and for 2) the cipher 8630Sstevel@tonic-gate * groups of cipher_aliases added together in one list (otherwise 8640Sstevel@tonic-gate * we would be happy with just the cipher_aliases table). 8650Sstevel@tonic-gate */ 8660Sstevel@tonic-gate num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER); 8670Sstevel@tonic-gate num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1; 8680Sstevel@tonic-gate ca_list = 8690Sstevel@tonic-gate (SSL_CIPHER **)OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max); 8700Sstevel@tonic-gate if (ca_list == NULL) 8710Sstevel@tonic-gate { 8720Sstevel@tonic-gate OPENSSL_free(co_list); 8730Sstevel@tonic-gate SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE); 8740Sstevel@tonic-gate return(NULL); /* Failure */ 8750Sstevel@tonic-gate } 8760Sstevel@tonic-gate ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, disabled_mask, 8770Sstevel@tonic-gate head); 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate /* 8800Sstevel@tonic-gate * If the rule_string begins with DEFAULT, apply the default rule 8810Sstevel@tonic-gate * before using the (possibly available) additional rules. 8820Sstevel@tonic-gate */ 8830Sstevel@tonic-gate ok = 1; 8840Sstevel@tonic-gate rule_p = rule_str; 8850Sstevel@tonic-gate if (strncmp(rule_str,"DEFAULT",7) == 0) 8860Sstevel@tonic-gate { 8870Sstevel@tonic-gate ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST, 8880Sstevel@tonic-gate co_list, &head, &tail, ca_list); 8890Sstevel@tonic-gate rule_p += 7; 8900Sstevel@tonic-gate if (*rule_p == ':') 8910Sstevel@tonic-gate rule_p++; 8920Sstevel@tonic-gate } 8930Sstevel@tonic-gate 8940Sstevel@tonic-gate if (ok && (strlen(rule_p) > 0)) 8950Sstevel@tonic-gate ok = ssl_cipher_process_rulestr(rule_p, co_list, &head, &tail, 8960Sstevel@tonic-gate ca_list); 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate OPENSSL_free(ca_list); /* Not needed anymore */ 8990Sstevel@tonic-gate 9000Sstevel@tonic-gate if (!ok) 9010Sstevel@tonic-gate { /* Rule processing failure */ 9020Sstevel@tonic-gate OPENSSL_free(co_list); 9030Sstevel@tonic-gate return(NULL); 9040Sstevel@tonic-gate } 9050Sstevel@tonic-gate /* 9060Sstevel@tonic-gate * Allocate new "cipherstack" for the result, return with error 9070Sstevel@tonic-gate * if we cannot get one. 9080Sstevel@tonic-gate */ 9090Sstevel@tonic-gate if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) 9100Sstevel@tonic-gate { 9110Sstevel@tonic-gate OPENSSL_free(co_list); 9120Sstevel@tonic-gate return(NULL); 9130Sstevel@tonic-gate } 9140Sstevel@tonic-gate 9150Sstevel@tonic-gate /* 9160Sstevel@tonic-gate * The cipher selection for the list is done. The ciphers are added 9170Sstevel@tonic-gate * to the resulting precedence to the STACK_OF(SSL_CIPHER). 9180Sstevel@tonic-gate */ 9190Sstevel@tonic-gate for (curr = head; curr != NULL; curr = curr->next) 9200Sstevel@tonic-gate { 9210Sstevel@tonic-gate if (curr->active) 9220Sstevel@tonic-gate { 9230Sstevel@tonic-gate sk_SSL_CIPHER_push(cipherstack, curr->cipher); 9240Sstevel@tonic-gate #ifdef CIPHER_DEBUG 9250Sstevel@tonic-gate printf("<%s>\n",curr->cipher->name); 9260Sstevel@tonic-gate #endif 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate } 9290Sstevel@tonic-gate OPENSSL_free(co_list); /* Not needed any longer */ 9300Sstevel@tonic-gate 931*2139Sjp161948 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack); 932*2139Sjp161948 if (tmp_cipher_list == NULL) 9330Sstevel@tonic-gate { 9340Sstevel@tonic-gate sk_SSL_CIPHER_free(cipherstack); 935*2139Sjp161948 return NULL; 9360Sstevel@tonic-gate } 937*2139Sjp161948 if (*cipher_list != NULL) 938*2139Sjp161948 sk_SSL_CIPHER_free(*cipher_list); 939*2139Sjp161948 *cipher_list = cipherstack; 940*2139Sjp161948 if (*cipher_list_by_id != NULL) 941*2139Sjp161948 sk_SSL_CIPHER_free(*cipher_list_by_id); 942*2139Sjp161948 *cipher_list_by_id = tmp_cipher_list; 9430Sstevel@tonic-gate sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp); 9440Sstevel@tonic-gate 9450Sstevel@tonic-gate return(cipherstack); 9460Sstevel@tonic-gate } 9470Sstevel@tonic-gate 9480Sstevel@tonic-gate char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len) 9490Sstevel@tonic-gate { 9500Sstevel@tonic-gate int is_export,pkl,kl; 951*2139Sjp161948 const char *ver,*exp_str; 952*2139Sjp161948 const char *kx,*au,*enc,*mac; 9530Sstevel@tonic-gate unsigned long alg,alg2,alg_s; 9540Sstevel@tonic-gate #ifdef KSSL_DEBUG 955*2139Sjp161948 static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s AL=%lx\n"; 9560Sstevel@tonic-gate #else 957*2139Sjp161948 static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s\n"; 9580Sstevel@tonic-gate #endif /* KSSL_DEBUG */ 9590Sstevel@tonic-gate 9600Sstevel@tonic-gate alg=cipher->algorithms; 9610Sstevel@tonic-gate alg_s=cipher->algo_strength; 9620Sstevel@tonic-gate alg2=cipher->algorithm2; 9630Sstevel@tonic-gate 9640Sstevel@tonic-gate is_export=SSL_C_IS_EXPORT(cipher); 9650Sstevel@tonic-gate pkl=SSL_C_EXPORT_PKEYLENGTH(cipher); 9660Sstevel@tonic-gate kl=SSL_C_EXPORT_KEYLENGTH(cipher); 9670Sstevel@tonic-gate exp_str=is_export?" export":""; 968*2139Sjp161948 9690Sstevel@tonic-gate if (alg & SSL_SSLV2) 9700Sstevel@tonic-gate ver="SSLv2"; 9710Sstevel@tonic-gate else if (alg & SSL_SSLV3) 9720Sstevel@tonic-gate ver="SSLv3"; 9730Sstevel@tonic-gate else 9740Sstevel@tonic-gate ver="unknown"; 9750Sstevel@tonic-gate 9760Sstevel@tonic-gate switch (alg&SSL_MKEY_MASK) 9770Sstevel@tonic-gate { 9780Sstevel@tonic-gate case SSL_kRSA: 9790Sstevel@tonic-gate kx=is_export?(pkl == 512 ? "RSA(512)" : "RSA(1024)"):"RSA"; 9800Sstevel@tonic-gate break; 9810Sstevel@tonic-gate case SSL_kDHr: 9820Sstevel@tonic-gate kx="DH/RSA"; 9830Sstevel@tonic-gate break; 9840Sstevel@tonic-gate case SSL_kDHd: 9850Sstevel@tonic-gate kx="DH/DSS"; 9860Sstevel@tonic-gate break; 9870Sstevel@tonic-gate case SSL_kKRB5: /* VRS */ 9880Sstevel@tonic-gate case SSL_KRB5: /* VRS */ 9890Sstevel@tonic-gate kx="KRB5"; 9900Sstevel@tonic-gate break; 9910Sstevel@tonic-gate case SSL_kFZA: 9920Sstevel@tonic-gate kx="Fortezza"; 9930Sstevel@tonic-gate break; 9940Sstevel@tonic-gate case SSL_kEDH: 9950Sstevel@tonic-gate kx=is_export?(pkl == 512 ? "DH(512)" : "DH(1024)"):"DH"; 9960Sstevel@tonic-gate break; 997*2139Sjp161948 case SSL_kECDH: 998*2139Sjp161948 case SSL_kECDHE: 999*2139Sjp161948 kx=is_export?"ECDH(<=163)":"ECDH"; 1000*2139Sjp161948 break; 10010Sstevel@tonic-gate default: 10020Sstevel@tonic-gate kx="unknown"; 10030Sstevel@tonic-gate } 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate switch (alg&SSL_AUTH_MASK) 10060Sstevel@tonic-gate { 10070Sstevel@tonic-gate case SSL_aRSA: 10080Sstevel@tonic-gate au="RSA"; 10090Sstevel@tonic-gate break; 10100Sstevel@tonic-gate case SSL_aDSS: 10110Sstevel@tonic-gate au="DSS"; 10120Sstevel@tonic-gate break; 10130Sstevel@tonic-gate case SSL_aDH: 10140Sstevel@tonic-gate au="DH"; 10150Sstevel@tonic-gate break; 10160Sstevel@tonic-gate case SSL_aKRB5: /* VRS */ 10170Sstevel@tonic-gate case SSL_KRB5: /* VRS */ 10180Sstevel@tonic-gate au="KRB5"; 10190Sstevel@tonic-gate break; 10200Sstevel@tonic-gate case SSL_aFZA: 10210Sstevel@tonic-gate case SSL_aNULL: 10220Sstevel@tonic-gate au="None"; 10230Sstevel@tonic-gate break; 1024*2139Sjp161948 case SSL_aECDSA: 1025*2139Sjp161948 au="ECDSA"; 1026*2139Sjp161948 break; 10270Sstevel@tonic-gate default: 10280Sstevel@tonic-gate au="unknown"; 10290Sstevel@tonic-gate break; 10300Sstevel@tonic-gate } 10310Sstevel@tonic-gate 10320Sstevel@tonic-gate switch (alg&SSL_ENC_MASK) 10330Sstevel@tonic-gate { 10340Sstevel@tonic-gate case SSL_DES: 10350Sstevel@tonic-gate enc=(is_export && kl == 5)?"DES(40)":"DES(56)"; 10360Sstevel@tonic-gate break; 10370Sstevel@tonic-gate case SSL_3DES: 10380Sstevel@tonic-gate enc="3DES(168)"; 10390Sstevel@tonic-gate break; 10400Sstevel@tonic-gate case SSL_RC4: 10410Sstevel@tonic-gate enc=is_export?(kl == 5 ? "RC4(40)" : "RC4(56)") 10420Sstevel@tonic-gate :((alg2&SSL2_CF_8_BYTE_ENC)?"RC4(64)":"RC4(128)"); 10430Sstevel@tonic-gate break; 10440Sstevel@tonic-gate case SSL_RC2: 10450Sstevel@tonic-gate enc=is_export?(kl == 5 ? "RC2(40)" : "RC2(56)"):"RC2(128)"; 10460Sstevel@tonic-gate break; 10470Sstevel@tonic-gate case SSL_IDEA: 10480Sstevel@tonic-gate enc="IDEA(128)"; 10490Sstevel@tonic-gate break; 10500Sstevel@tonic-gate case SSL_eFZA: 10510Sstevel@tonic-gate enc="Fortezza"; 10520Sstevel@tonic-gate break; 10530Sstevel@tonic-gate case SSL_eNULL: 10540Sstevel@tonic-gate enc="None"; 10550Sstevel@tonic-gate break; 10560Sstevel@tonic-gate case SSL_AES: 10570Sstevel@tonic-gate switch(cipher->strength_bits) 10580Sstevel@tonic-gate { 10590Sstevel@tonic-gate case 128: enc="AES(128)"; break; 10600Sstevel@tonic-gate case 192: enc="AES(192)"; break; 10610Sstevel@tonic-gate case 256: enc="AES(256)"; break; 10620Sstevel@tonic-gate default: enc="AES(?""?""?)"; break; 10630Sstevel@tonic-gate } 10640Sstevel@tonic-gate break; 10650Sstevel@tonic-gate default: 10660Sstevel@tonic-gate enc="unknown"; 10670Sstevel@tonic-gate break; 10680Sstevel@tonic-gate } 10690Sstevel@tonic-gate 10700Sstevel@tonic-gate switch (alg&SSL_MAC_MASK) 10710Sstevel@tonic-gate { 10720Sstevel@tonic-gate case SSL_MD5: 10730Sstevel@tonic-gate mac="MD5"; 10740Sstevel@tonic-gate break; 10750Sstevel@tonic-gate case SSL_SHA1: 10760Sstevel@tonic-gate mac="SHA1"; 10770Sstevel@tonic-gate break; 10780Sstevel@tonic-gate default: 10790Sstevel@tonic-gate mac="unknown"; 10800Sstevel@tonic-gate break; 10810Sstevel@tonic-gate } 10820Sstevel@tonic-gate 10830Sstevel@tonic-gate if (buf == NULL) 10840Sstevel@tonic-gate { 10850Sstevel@tonic-gate len=128; 10860Sstevel@tonic-gate buf=OPENSSL_malloc(len); 10870Sstevel@tonic-gate if (buf == NULL) return("OPENSSL_malloc Error"); 10880Sstevel@tonic-gate } 10890Sstevel@tonic-gate else if (len < 128) 10900Sstevel@tonic-gate return("Buffer too small"); 10910Sstevel@tonic-gate 10920Sstevel@tonic-gate #ifdef KSSL_DEBUG 10930Sstevel@tonic-gate BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str,alg); 10940Sstevel@tonic-gate #else 10950Sstevel@tonic-gate BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str); 10960Sstevel@tonic-gate #endif /* KSSL_DEBUG */ 10970Sstevel@tonic-gate return(buf); 10980Sstevel@tonic-gate } 10990Sstevel@tonic-gate 1100*2139Sjp161948 char *SSL_CIPHER_get_version(const SSL_CIPHER *c) 11010Sstevel@tonic-gate { 11020Sstevel@tonic-gate int i; 11030Sstevel@tonic-gate 11040Sstevel@tonic-gate if (c == NULL) return("(NONE)"); 11050Sstevel@tonic-gate i=(int)(c->id>>24L); 11060Sstevel@tonic-gate if (i == 3) 11070Sstevel@tonic-gate return("TLSv1/SSLv3"); 11080Sstevel@tonic-gate else if (i == 2) 11090Sstevel@tonic-gate return("SSLv2"); 11100Sstevel@tonic-gate else 11110Sstevel@tonic-gate return("unknown"); 11120Sstevel@tonic-gate } 11130Sstevel@tonic-gate 11140Sstevel@tonic-gate /* return the actual cipher being used */ 1115*2139Sjp161948 const char *SSL_CIPHER_get_name(const SSL_CIPHER *c) 11160Sstevel@tonic-gate { 11170Sstevel@tonic-gate if (c != NULL) 11180Sstevel@tonic-gate return(c->name); 11190Sstevel@tonic-gate return("(NONE)"); 11200Sstevel@tonic-gate } 11210Sstevel@tonic-gate 11220Sstevel@tonic-gate /* number of bits for symmetric cipher */ 1123*2139Sjp161948 int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits) 11240Sstevel@tonic-gate { 11250Sstevel@tonic-gate int ret=0; 11260Sstevel@tonic-gate 11270Sstevel@tonic-gate if (c != NULL) 11280Sstevel@tonic-gate { 11290Sstevel@tonic-gate if (alg_bits != NULL) *alg_bits = c->alg_bits; 11300Sstevel@tonic-gate ret = c->strength_bits; 11310Sstevel@tonic-gate } 11320Sstevel@tonic-gate return(ret); 11330Sstevel@tonic-gate } 11340Sstevel@tonic-gate 11350Sstevel@tonic-gate SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n) 11360Sstevel@tonic-gate { 11370Sstevel@tonic-gate SSL_COMP *ctmp; 11380Sstevel@tonic-gate int i,nn; 11390Sstevel@tonic-gate 11400Sstevel@tonic-gate if ((n == 0) || (sk == NULL)) return(NULL); 11410Sstevel@tonic-gate nn=sk_SSL_COMP_num(sk); 11420Sstevel@tonic-gate for (i=0; i<nn; i++) 11430Sstevel@tonic-gate { 11440Sstevel@tonic-gate ctmp=sk_SSL_COMP_value(sk,i); 11450Sstevel@tonic-gate if (ctmp->id == n) 11460Sstevel@tonic-gate return(ctmp); 11470Sstevel@tonic-gate } 11480Sstevel@tonic-gate return(NULL); 11490Sstevel@tonic-gate } 11500Sstevel@tonic-gate 1151*2139Sjp161948 #ifdef OPENSSL_NO_COMP 1152*2139Sjp161948 void *SSL_COMP_get_compression_methods(void) 11530Sstevel@tonic-gate { 1154*2139Sjp161948 return NULL; 1155*2139Sjp161948 } 1156*2139Sjp161948 int SSL_COMP_add_compression_method(int id, void *cm) 1157*2139Sjp161948 { 1158*2139Sjp161948 return 1; 11590Sstevel@tonic-gate } 11600Sstevel@tonic-gate 1161*2139Sjp161948 const char *SSL_COMP_get_name(const void *comp) 1162*2139Sjp161948 { 1163*2139Sjp161948 return NULL; 1164*2139Sjp161948 } 1165*2139Sjp161948 #else 11660Sstevel@tonic-gate STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void) 11670Sstevel@tonic-gate { 1168*2139Sjp161948 load_builtin_compressions(); 11690Sstevel@tonic-gate return(ssl_comp_methods); 11700Sstevel@tonic-gate } 11710Sstevel@tonic-gate 11720Sstevel@tonic-gate int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) 11730Sstevel@tonic-gate { 11740Sstevel@tonic-gate SSL_COMP *comp; 11750Sstevel@tonic-gate 11760Sstevel@tonic-gate if (cm == NULL || cm->type == NID_undef) 11770Sstevel@tonic-gate return 1; 11780Sstevel@tonic-gate 1179*2139Sjp161948 /* According to draft-ietf-tls-compression-04.txt, the 1180*2139Sjp161948 compression number ranges should be the following: 1181*2139Sjp161948 1182*2139Sjp161948 0 to 63: methods defined by the IETF 1183*2139Sjp161948 64 to 192: external party methods assigned by IANA 1184*2139Sjp161948 193 to 255: reserved for private use */ 1185*2139Sjp161948 if (id < 193 || id > 255) 1186*2139Sjp161948 { 1187*2139Sjp161948 SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,SSL_R_COMPRESSION_ID_NOT_WITHIN_PRIVATE_RANGE); 1188*2139Sjp161948 return 0; 1189*2139Sjp161948 } 1190*2139Sjp161948 11910Sstevel@tonic-gate MemCheck_off(); 11920Sstevel@tonic-gate comp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP)); 11930Sstevel@tonic-gate comp->id=id; 11940Sstevel@tonic-gate comp->method=cm; 1195*2139Sjp161948 load_builtin_compressions(); 1196*2139Sjp161948 if (ssl_comp_methods 1197*2139Sjp161948 && !sk_SSL_COMP_find(ssl_comp_methods,comp)) 11980Sstevel@tonic-gate { 1199*2139Sjp161948 OPENSSL_free(comp); 1200*2139Sjp161948 MemCheck_on(); 1201*2139Sjp161948 SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,SSL_R_DUPLICATE_COMPRESSION_ID); 1202*2139Sjp161948 return(1); 1203*2139Sjp161948 } 1204*2139Sjp161948 else if ((ssl_comp_methods == NULL) 1205*2139Sjp161948 || !sk_SSL_COMP_push(ssl_comp_methods,comp)) 1206*2139Sjp161948 { 1207*2139Sjp161948 OPENSSL_free(comp); 12080Sstevel@tonic-gate MemCheck_on(); 12090Sstevel@tonic-gate SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,ERR_R_MALLOC_FAILURE); 12100Sstevel@tonic-gate return(1); 12110Sstevel@tonic-gate } 12120Sstevel@tonic-gate else 12130Sstevel@tonic-gate { 12140Sstevel@tonic-gate MemCheck_on(); 12150Sstevel@tonic-gate return(0); 12160Sstevel@tonic-gate } 12170Sstevel@tonic-gate } 1218*2139Sjp161948 1219*2139Sjp161948 const char *SSL_COMP_get_name(const COMP_METHOD *comp) 1220*2139Sjp161948 { 1221*2139Sjp161948 if (comp) 1222*2139Sjp161948 return comp->name; 1223*2139Sjp161948 return NULL; 1224*2139Sjp161948 } 1225*2139Sjp161948 1226*2139Sjp161948 #endif 1227