📄 ssl_ciph.c
字号:
const char *rule_p; CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr; SSL_CIPHER **ca_list = NULL; /* * Return with error if nothing to do. */ if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL) return NULL; /* * To reduce the work to do we only want to process the compiled * in algorithms, so we first get the mask of disabled ciphers. */ disabled_mask = ssl_cipher_get_disabled(); /* * Now we have to collect the available ciphers from the compiled * in ciphers. We cannot get more than the number compiled in, so * it is used for allocation. */ num_of_ciphers = ssl_method->num_ciphers();#ifdef KSSL_DEBUG printf("ssl_create_cipher_list() for %d ciphers\n", num_of_ciphers);#endif /* KSSL_DEBUG */ co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers); if (co_list == NULL) { SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE); return(NULL); /* Failure */ } ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, disabled_mask, co_list, &head, &tail); /* * We also need cipher aliases for selecting based on the rule_str. * There might be two types of entries in the rule_str: 1) names * of ciphers themselves 2) aliases for groups of ciphers. * For 1) we need the available ciphers and for 2) the cipher * groups of cipher_aliases added together in one list (otherwise * we would be happy with just the cipher_aliases table). */ num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER); num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1; ca_list = (SSL_CIPHER **)OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max); if (ca_list == NULL) { OPENSSL_free(co_list); SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE); return(NULL); /* Failure */ } ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, disabled_mask, head); /* * If the rule_string begins with DEFAULT, apply the default rule * before using the (possibly available) additional rules. */ ok = 1; rule_p = rule_str; if (strncmp(rule_str,"DEFAULT",7) == 0) { ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST, co_list, &head, &tail, ca_list); rule_p += 7; if (*rule_p == ':') rule_p++; } if (ok && (strlen(rule_p) > 0)) ok = ssl_cipher_process_rulestr(rule_p, co_list, &head, &tail, ca_list); OPENSSL_free(ca_list); /* Not needed anymore */ if (!ok) { /* Rule processing failure */ OPENSSL_free(co_list); return(NULL); } /* * Allocate new "cipherstack" for the result, return with error * if we cannot get one. */ if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) { OPENSSL_free(co_list); return(NULL); } /* * The cipher selection for the list is done. The ciphers are added * to the resulting precedence to the STACK_OF(SSL_CIPHER). */ for (curr = head; curr != NULL; curr = curr->next) { if (curr->active) { sk_SSL_CIPHER_push(cipherstack, curr->cipher);#ifdef CIPHER_DEBUG printf("<%s>\n",curr->cipher->name);#endif } } OPENSSL_free(co_list); /* Not needed any longer */ tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack); if (tmp_cipher_list == NULL) { sk_SSL_CIPHER_free(cipherstack); return NULL; } if (*cipher_list != NULL) sk_SSL_CIPHER_free(*cipher_list); *cipher_list = cipherstack; if (*cipher_list_by_id != NULL) sk_SSL_CIPHER_free(*cipher_list_by_id); *cipher_list_by_id = tmp_cipher_list; sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp); return(cipherstack); }char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len) { int is_export,pkl,kl; const char *ver,*exp_str; const char *kx,*au,*enc,*mac; unsigned long alg,alg2,alg_s;#ifdef KSSL_DEBUG static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s AL=%lx\n";#else static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s\n";#endif /* KSSL_DEBUG */ alg=cipher->algorithms; alg_s=cipher->algo_strength; alg2=cipher->algorithm2; is_export=SSL_C_IS_EXPORT(cipher); pkl=SSL_C_EXPORT_PKEYLENGTH(cipher); kl=SSL_C_EXPORT_KEYLENGTH(cipher); exp_str=is_export?" export":""; if (alg & SSL_SSLV2) ver="SSLv2"; else if (alg & SSL_SSLV3) ver="SSLv3"; else ver="unknown"; switch (alg&SSL_MKEY_MASK) { case SSL_kRSA: kx=is_export?(pkl == 512 ? "RSA(512)" : "RSA(1024)"):"RSA"; break; case SSL_kDHr: kx="DH/RSA"; break; case SSL_kDHd: kx="DH/DSS"; break; case SSL_kKRB5: /* VRS */ case SSL_KRB5: /* VRS */ kx="KRB5"; break; case SSL_kFZA: kx="Fortezza"; break; case SSL_kEDH: kx=is_export?(pkl == 512 ? "DH(512)" : "DH(1024)"):"DH"; break; case SSL_kECDH: case SSL_kECDHE: kx=is_export?"ECDH(<=163)":"ECDH"; break; default: kx="unknown"; } switch (alg&SSL_AUTH_MASK) { case SSL_aRSA: au="RSA"; break; case SSL_aDSS: au="DSS"; break; case SSL_aDH: au="DH"; break; case SSL_aKRB5: /* VRS */ case SSL_KRB5: /* VRS */ au="KRB5"; break; case SSL_aFZA: case SSL_aNULL: au="None"; break; case SSL_aECDSA: au="ECDSA"; break; default: au="unknown"; break; } switch (alg&SSL_ENC_MASK) { case SSL_DES: enc=(is_export && kl == 5)?"DES(40)":"DES(56)"; break; case SSL_3DES: enc="3DES(168)"; break; case SSL_RC4: enc=is_export?(kl == 5 ? "RC4(40)" : "RC4(56)") :((alg2&SSL2_CF_8_BYTE_ENC)?"RC4(64)":"RC4(128)"); break; case SSL_RC2: enc=is_export?(kl == 5 ? "RC2(40)" : "RC2(56)"):"RC2(128)"; break; case SSL_IDEA: enc="IDEA(128)"; break; case SSL_eFZA: enc="Fortezza"; break; case SSL_eNULL: enc="None"; break; case SSL_AES: switch(cipher->strength_bits) { case 128: enc="AES(128)"; break; case 192: enc="AES(192)"; break; case 256: enc="AES(256)"; break; default: enc="AES(?""?""?)"; break; } break; default: enc="unknown"; break; } switch (alg&SSL_MAC_MASK) { case SSL_MD5: mac="MD5"; break; case SSL_SHA1: mac="SHA1"; break; default: mac="unknown"; break; } if (buf == NULL) { len=128; buf=OPENSSL_malloc(len); if (buf == NULL) return("OPENSSL_malloc Error"); } else if (len < 128) return("Buffer too small");#ifdef KSSL_DEBUG BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str,alg);#else BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str);#endif /* KSSL_DEBUG */ return(buf); }char *SSL_CIPHER_get_version(const SSL_CIPHER *c) { int i; if (c == NULL) return("(NONE)"); i=(int)(c->id>>24L); if (i == 3) return("TLSv1/SSLv3"); else if (i == 2) return("SSLv2"); else return("unknown"); }/* return the actual cipher being used */const char *SSL_CIPHER_get_name(const SSL_CIPHER *c) { if (c != NULL) return(c->name); return("(NONE)"); }/* number of bits for symmetric cipher */int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits) { int ret=0; if (c != NULL) { if (alg_bits != NULL) *alg_bits = c->alg_bits; ret = c->strength_bits; } return(ret); }SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n) { SSL_COMP *ctmp; int i,nn; if ((n == 0) || (sk == NULL)) return(NULL); nn=sk_SSL_COMP_num(sk); for (i=0; i<nn; i++) { ctmp=sk_SSL_COMP_value(sk,i); if (ctmp->id == n) return(ctmp); } return(NULL); }#ifdef OPENSSL_NO_COMPvoid *SSL_COMP_get_compression_methods(void) { return NULL; }int SSL_COMP_add_compression_method(int id, void *cm) { return 1; }const char *SSL_COMP_get_name(const void *comp) { return NULL; }#elseSTACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void) { load_builtin_compressions(); return(ssl_comp_methods); }int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { SSL_COMP *comp; if (cm == NULL || cm->type == NID_undef) return 1; /* According to draft-ietf-tls-compression-04.txt, the compression number ranges should be the following: 0 to 63: methods defined by the IETF 64 to 192: external party methods assigned by IANA 193 to 255: reserved for private use */ if (id < 193 || id > 255) { SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,SSL_R_COMPRESSION_ID_NOT_WITHIN_PRIVATE_RANGE); return 0; } MemCheck_off(); comp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP)); comp->id=id; comp->method=cm; load_builtin_compressions(); if (ssl_comp_methods && !sk_SSL_COMP_find(ssl_comp_methods,comp)) { OPENSSL_free(comp); MemCheck_on(); SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,SSL_R_DUPLICATE_COMPRESSION_ID); return(1); } else if ((ssl_comp_methods == NULL) || !sk_SSL_COMP_push(ssl_comp_methods,comp)) { OPENSSL_free(comp); MemCheck_on(); SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,ERR_R_MALLOC_FAILURE); return(1); } else { MemCheck_on(); return(0); } }const char *SSL_COMP_get_name(const COMP_METHOD *comp) { if (comp) return comp->name; return NULL; }#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -