summaryrefslogtreecommitdiff
path: root/src/lib/libssl/ssl_ciph.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/ssl_ciph.c')
-rw-r--r--src/lib/libssl/ssl_ciph.c825
1 files changed, 529 insertions, 296 deletions
diff --git a/src/lib/libssl/ssl_ciph.c b/src/lib/libssl/ssl_ciph.c
index 4c2989c47a..1cbc2886e9 100644
--- a/src/lib/libssl/ssl_ciph.c
+++ b/src/lib/libssl/ssl_ciph.c
@@ -83,24 +83,11 @@ static const EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX]={
83 NULL,NULL, 83 NULL,NULL,
84 }; 84 };
85 85
86typedef struct cipher_sort_st
87 {
88 SSL_CIPHER *cipher;
89 int pref;
90 } CIPHER_SORT;
91
92#define CIPHER_ADD 1 86#define CIPHER_ADD 1
93#define CIPHER_KILL 2 87#define CIPHER_KILL 2
94#define CIPHER_DEL 3 88#define CIPHER_DEL 3
95#define CIPHER_ORD 4 89#define CIPHER_ORD 4
96 90#define CIPHER_SPECIAL 5
97typedef struct cipher_choice_st
98 {
99 int type;
100 unsigned long algorithms;
101 unsigned long mask;
102 long top;
103 } CIPHER_CHOICE;
104 91
105typedef struct cipher_order_st 92typedef struct cipher_order_st
106 { 93 {
@@ -110,59 +97,55 @@ typedef struct cipher_order_st
110 struct cipher_order_st *next,*prev; 97 struct cipher_order_st *next,*prev;
111 } CIPHER_ORDER; 98 } CIPHER_ORDER;
112 99
113static SSL_CIPHER cipher_aliases[]={ 100static const SSL_CIPHER cipher_aliases[]={
114 /* Don't include eNULL unless specifically enabled */ 101 /* Don't include eNULL unless specifically enabled */
115 {0,SSL_TXT_ALL, 0,SSL_ALL & ~SSL_eNULL, 0,SSL_ALL}, /* must be first */ 102 {0,SSL_TXT_ALL, 0,SSL_ALL & ~SSL_eNULL, SSL_ALL ,0,0,0,SSL_ALL,SSL_ALL}, /* must be first */
116 {0,SSL_TXT_kRSA,0,SSL_kRSA, 0,SSL_MKEY_MASK}, 103 {0,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SL_TXT_SSLV2, 0,SSL_SSLV2, 0,SSL_SSL_MASK}, 138
152 {0,SSL_TXT_SSLV3, 0,SSL_SSLV3, 0,SSL_SSL_MASK}, 139 {0,SSL_TXT_EXP ,0, 0,SSL_EXPORT, 0,0,0,0,SSL_EXP_MASK},
153 {0,SSL_TXT_TLSV1, 0,SSL_TLSV1, 0,SSL_SSL_MASK}, 140 {0,SSL_TXT_EXPORT,0, 0,SSL_EXPORT, 0,0,0,0,SSL_EXP_MASK},
154 {0,SSL_TXT_LOW, 0,SSL_LOW, 0,SSL_STRONG_MASK}, 141 {0,SSL_TXT_EXP40, 0, 0, SSL_EXP40, 0,0,0,0,SSL_STRONG_MASK},
155 {0,SSL_TXT_MEDIUM,0,SSL_MEDIUM,0,SSL_STRONG_MASK}, 142 {0,SSL_TXT_EXP56, 0, 0, SSL_EXP56, 0,0,0,0,SSL_STRONG_MASK},
156 {0,SSL_TXT_HIGH, 0,SSL_HIGH, 0,SSL_STRONG_MASK}, 143 {0,SSL_TXT_LOW, 0, 0, SSL_LOW, 0,0,0,0,SSL_STRONG_MASK},
144 {0,SSL_TXT_MEDIUM,0, 0,SSL_MEDIUM, 0,0,0,0,SSL_STRONG_MASK},
145 {0,SSL_TXT_HIGH, 0, 0, SSL_HIGH, 0,0,0,0,SSL_STRONG_MASK},
157 }; 146 };
158 147
159static int init_ciphers=1; 148static int init_ciphers=1;
160static void load_ciphers();
161
162static int cmp_by_name(SSL_CIPHER **a, SSL_CIPHER **b)
163 {
164 return(strcmp((*a)->name,(*b)->name));
165 }
166 149
167static void load_ciphers(void) 150static void load_ciphers(void)
168 { 151 {
@@ -294,170 +277,320 @@ static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
294 *tail=curr; 277 *tail=curr;
295 } 278 }
296 279
297STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(SSL_METHOD *ssl_method, 280static unsigned long ssl_cipher_get_disabled(void)
298 STACK_OF(SSL_CIPHER) **cipher_list,
299 STACK_OF(SSL_CIPHER) **cipher_list_by_id,
300 char *str)
301 { 281 {
302 SSL_CIPHER *c; 282 unsigned long mask;
303 char *l;
304 STACK_OF(SSL_CIPHER) *ret=NULL,*ok=NULL;
305#define CL_BUF 40
306 char buf[CL_BUF];
307 char *tmp_str=NULL;
308 unsigned long mask,algorithms,ma;
309 char *start;
310 int i,j,k,num=0,ch,multi;
311 unsigned long al;
312 STACK *ca_list=NULL;
313 int current_x,num_x;
314 CIPHER_CHOICE *ops=NULL;
315 CIPHER_ORDER *list=NULL,*head=NULL,*tail=NULL,*curr,*tail2,*curr2;
316 int list_num;
317 int type;
318 SSL_CIPHER c_tmp,*cp;
319
320 if (str == NULL) return(NULL);
321
322 if (strncmp(str,"DEFAULT",7) == 0)
323 {
324 i=strlen(str)+2+strlen(SSL_DEFAULT_CIPHER_LIST);
325 if ((tmp_str=Malloc(i)) == NULL)
326 {
327 SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
328 goto err;
329 }
330 strcpy(tmp_str,SSL_DEFAULT_CIPHER_LIST);
331 strcat(tmp_str,":");
332 strcat(tmp_str,&(str[7]));
333 str=tmp_str;
334 }
335 if (init_ciphers) load_ciphers();
336
337 num=ssl_method->num_ciphers();
338
339 if ((ret=sk_SSL_CIPHER_new(NULL)) == NULL) goto err;
340 if ((ca_list=(STACK *)sk_new(cmp_by_name)) == NULL) goto err;
341 283
342 mask =SSL_kFZA; 284 mask = SSL_kFZA;
343#ifdef NO_RSA 285#ifdef NO_RSA
344 mask|=SSL_aRSA|SSL_kRSA; 286 mask |= SSL_aRSA|SSL_kRSA;
345#endif 287#endif
346#ifdef NO_DSA 288#ifdef NO_DSA
347 mask|=SSL_aDSS; 289 mask |= SSL_aDSS;
348#endif 290#endif
349#ifdef NO_DH 291#ifdef NO_DH
350 mask|=SSL_kDHr|SSL_kDHd|SSL_kEDH|SSL_aDH; 292 mask |= SSL_kDHr|SSL_kDHd|SSL_kEDH|SSL_aDH;
351#endif 293#endif
352 294
353#ifdef SSL_FORBID_ENULL 295#ifdef SSL_FORBID_ENULL
354 mask|=SSL_eNULL; 296 mask |= SSL_eNULL;
355#endif 297#endif
356 298
357 mask|=(ssl_cipher_methods[SSL_ENC_DES_IDX ] == NULL)?SSL_DES :0; 299 mask |= (ssl_cipher_methods[SSL_ENC_DES_IDX ] == NULL) ? SSL_DES :0;
358 mask|=(ssl_cipher_methods[SSL_ENC_3DES_IDX] == NULL)?SSL_3DES:0; 300 mask |= (ssl_cipher_methods[SSL_ENC_3DES_IDX] == NULL) ? SSL_3DES:0;
359 mask|=(ssl_cipher_methods[SSL_ENC_RC4_IDX ] == NULL)?SSL_RC4 :0; 301 mask |= (ssl_cipher_methods[SSL_ENC_RC4_IDX ] == NULL) ? SSL_RC4 :0;
360 mask|=(ssl_cipher_methods[SSL_ENC_RC2_IDX ] == NULL)?SSL_RC2 :0; 302 mask |= (ssl_cipher_methods[SSL_ENC_RC2_IDX ] == NULL) ? SSL_RC2 :0;
361 mask|=(ssl_cipher_methods[SSL_ENC_IDEA_IDX] == NULL)?SSL_IDEA:0; 303 mask |= (ssl_cipher_methods[SSL_ENC_IDEA_IDX] == NULL) ? SSL_IDEA:0;
362 mask|=(ssl_cipher_methods[SSL_ENC_eFZA_IDX] == NULL)?SSL_eFZA:0; 304 mask |= (ssl_cipher_methods[SSL_ENC_eFZA_IDX] == NULL) ? SSL_eFZA:0;
305
306 mask |= (ssl_digest_methods[SSL_MD_MD5_IDX ] == NULL) ? SSL_MD5 :0;
307 mask |= (ssl_digest_methods[SSL_MD_SHA1_IDX] == NULL) ? SSL_SHA1:0;
363 308
364 mask|=(ssl_digest_methods[SSL_MD_MD5_IDX ] == NULL)?SSL_MD5 :0; 309 return(mask);
365 mask|=(ssl_digest_methods[SSL_MD_SHA1_IDX] == NULL)?SSL_SHA1:0; 310 }
311
312static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
313 int num_of_ciphers, unsigned long mask, CIPHER_ORDER *list,
314 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
315 {
316 int i, list_num;
317 SSL_CIPHER *c;
366 318
367 if ((list=(CIPHER_ORDER *)Malloc(sizeof(CIPHER_ORDER)*num)) == NULL) 319 /*
368 goto err; 320 * We have num_of_ciphers descriptions compiled in, depending on the
321 * method selected (SSLv2 and/or SSLv3, TLSv1 etc).
322 * These will later be sorted in a linked list with at most num
323 * entries.
324 */
369 325
370 /* Get the initial list of ciphers */ 326 /* Get the initial list of ciphers */
371 list_num=0; 327 list_num = 0; /* actual count of ciphers */
372 for (i=0; i<num; i++) 328 for (i = 0; i < num_of_ciphers; i++)
373 { 329 {
374 c=ssl_method->get_cipher((unsigned int)i); 330 c = ssl_method->get_cipher(i);
375 /* drop those that use any of that is not available */ 331 /* drop those that use any of that is not available */
376 if ((c != NULL) && c->valid && !(c->algorithms & mask)) 332 if ((c != NULL) && c->valid && !(c->algorithms & mask))
377 { 333 {
378 list[list_num].cipher=c; 334 list[list_num].cipher = c;
379 list[list_num].next=NULL; 335 list[list_num].next = NULL;
380 list[list_num].prev=NULL; 336 list[list_num].prev = NULL;
381 list[list_num].active=0; 337 list[list_num].active = 0;
382 list_num++; 338 list_num++;
339 /*
383 if (!sk_push(ca_list,(char *)c)) goto err; 340 if (!sk_push(ca_list,(char *)c)) goto err;
341 */
384 } 342 }
385 } 343 }
386 344
387 for (i=1; i<list_num-1; i++) 345 /*
346 * Prepare linked list from list entries
347 */
348 for (i = 1; i < list_num - 1; i++)
388 { 349 {
389 list[i].prev= &(list[i-1]); 350 list[i].prev = &(list[i-1]);
390 list[i].next= &(list[i+1]); 351 list[i].next = &(list[i+1]);
391 } 352 }
392 if (list_num > 0) 353 if (list_num > 0)
393 { 354 {
394 head= &(list[0]); 355 (*head_p) = &(list[0]);
395 head->prev=NULL; 356 (*head_p)->prev = NULL;
396 head->next= &(list[1]); 357 (*head_p)->next = &(list[1]);
397 tail= &(list[list_num-1]); 358 (*tail_p) = &(list[list_num - 1]);
398 tail->prev= &(list[list_num-2]); 359 (*tail_p)->prev = &(list[list_num - 2]);
399 tail->next=NULL; 360 (*tail_p)->next = NULL;
400 } 361 }
362 }
401 363
402 /* special case */ 364static void ssl_cipher_collect_aliases(SSL_CIPHER **ca_list,
403 cipher_aliases[0].algorithms &= ~mask; 365 int num_of_group_aliases, unsigned long mask,
366 CIPHER_ORDER *head)
367 {
368 CIPHER_ORDER *ciph_curr;
369 SSL_CIPHER **ca_curr;
370 int i;
404 371
405 /* get the aliases */ 372 /*
406 k=sizeof(cipher_aliases)/sizeof(SSL_CIPHER); 373 * First, add the real ciphers as already collected
407 for (j=0; j<k; j++) 374 */
375 ciph_curr = head;
376 ca_curr = ca_list;
377 while (ciph_curr != NULL)
408 { 378 {
409 al=cipher_aliases[j].algorithms; 379 *ca_curr = ciph_curr->cipher;
410 /* Drop those that are not relevent */ 380 ca_curr++;
411 if ((al & mask) == al) continue; 381 ciph_curr = ciph_curr->next;
412 if (!sk_push(ca_list,(char *)&(cipher_aliases[j]))) goto err;
413 } 382 }
414 383
415 /* ca_list now holds a 'stack' of SSL_CIPHERS, some real, some 384 /*
416 * 'aliases' */ 385 * Now we add the available ones from the cipher_aliases[] table.
386 * They represent either an algorithm, that must be fully
387 * supported (not match any bit in mask) or represent a cipher
388 * strength value (will be added in any case because algorithms=0).
389 */
390 for (i = 0; i < num_of_group_aliases; i++)
391 {
392 if ((i == 0) || /* always fetch "ALL" */
393 !(cipher_aliases[i].algorithms & mask))
394 {
395 *ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
396 ca_curr++;
397 }
398 }
417 399
418 /* how many parameters are there? */ 400 *ca_curr = NULL; /* end of list */
419 num=1; 401 }
420 for (l=str; *l; l++)
421 if (ITEM_SEP(*l))
422 num++;
423 ops=(CIPHER_CHOICE *)Malloc(sizeof(CIPHER_CHOICE)*num);
424 if (ops == NULL) goto err;
425 memset(ops,0,sizeof(CIPHER_CHOICE)*num);
426 402
427 /* we now parse the input string and create our operations */ 403static void ssl_cipher_apply_rule(unsigned long algorithms, unsigned long mask,
428 l=str; 404 unsigned long algo_strength, unsigned long mask_strength,
429 i=0; 405 int rule, int strength_bits, CIPHER_ORDER *list,
430 current_x=0; 406 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
407 {
408 CIPHER_ORDER *head, *tail, *curr, *curr2, *tail2;
409 SSL_CIPHER *cp;
410 unsigned long ma, ma_s;
431 411
412#ifdef CIPHER_DEBUG
413 printf("Applying rule %d with %08lx %08lx %08lx %08lx (%d)\n",
414 rule, algorithms, mask, algo_strength, mask_strength,
415 strength_bits);
416#endif
417
418 curr = head = *head_p;
419 curr2 = head;
420 tail2 = tail = *tail_p;
432 for (;;) 421 for (;;)
433 { 422 {
434 ch= *l; 423 if ((curr == NULL) || (curr == tail2)) break;
424 curr = curr2;
425 curr2 = curr->next;
426
427 cp = curr->cipher;
428
429 /*
430 * Selection criteria is either the number of strength_bits
431 * or the algorithm used.
432 */
433 if (strength_bits == -1)
434 {
435 ma = mask & cp->algorithms;
436 ma_s = mask_strength & cp->algo_strength;
437
438#ifdef CIPHER_DEBUG
439 printf("\nName: %s:\nAlgo = %08lx Algo_strength = %08lx\nMask = %08lx Mask_strength %08lx\n", cp->name, cp->algorithms, cp->algo_strength, mask, mask_strength);
440 printf("ma = %08lx ma_s %08lx, ma&algo=%08lx, ma_s&algos=%08lx\n", ma, ma_s, ma&algorithms, ma_s&algo_strength);
441#endif
442 /*
443 * Select: if none of the mask bit was met from the
444 * cipher or not all of the bits were met, the
445 * selection does not apply.
446 */
447 if (((ma == 0) && (ma_s == 0)) ||
448 ((ma & algorithms) != ma) ||
449 ((ma_s & algo_strength) != ma_s))
450 continue; /* does not apply */
451 }
452 else if (strength_bits != cp->strength_bits)
453 continue; /* does not apply */
454
455#ifdef CIPHER_DEBUG
456 printf("Action = %d\n", rule);
457#endif
458
459 /* add the cipher if it has not been added yet. */
460 if (rule == CIPHER_ADD)
461 {
462 if (!curr->active)
463 {
464 ll_append_tail(&head, curr, &tail);
465 curr->active = 1;
466 }
467 }
468 /* Move the added cipher to this location */
469 else if (rule == CIPHER_ORD)
470 {
471 if (curr->active)
472 {
473 ll_append_tail(&head, curr, &tail);
474 }
475 }
476 else if (rule == CIPHER_DEL)
477 curr->active = 0;
478 else if (rule == CIPHER_KILL)
479 {
480 if (head == curr)
481 head = curr->next;
482 else
483 curr->prev->next = curr->next;
484 if (tail == curr)
485 tail = curr->prev;
486 curr->active = 0;
487 if (curr->next != NULL)
488 curr->next->prev = curr->prev;
489 if (curr->prev != NULL)
490 curr->prev->next = curr->next;
491 curr->next = NULL;
492 curr->prev = NULL;
493 }
494 }
495
496 *head_p = head;
497 *tail_p = tail;
498 }
499
500static int ssl_cipher_strength_sort(CIPHER_ORDER *list, CIPHER_ORDER **head_p,
501 CIPHER_ORDER **tail_p)
502 {
503 int max_strength_bits, i, *number_uses;
504 CIPHER_ORDER *curr;
505
506 /*
507 * This routine sorts the ciphers with descending strength. The sorting
508 * must keep the pre-sorted sequence, so we apply the normal sorting
509 * routine as '+' movement to the end of the list.
510 */
511 max_strength_bits = 0;
512 curr = *head_p;
513 while (curr != NULL)
514 {
515 if (curr->active &&
516 (curr->cipher->strength_bits > max_strength_bits))
517 max_strength_bits = curr->cipher->strength_bits;
518 curr = curr->next;
519 }
520
521 number_uses = Malloc((max_strength_bits + 1) * sizeof(int));
522 if (!number_uses)
523 {
524 SSLerr(SSL_F_SSL_CIPHER_STRENGTH_SORT,ERR_R_MALLOC_FAILURE);
525 return(0);
526 }
527 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
528
529 /*
530 * Now find the strength_bits values actually used
531 */
532 curr = *head_p;
533 while (curr != NULL)
534 {
535 if (curr->active)
536 number_uses[curr->cipher->strength_bits]++;
537 curr = curr->next;
538 }
539 /*
540 * Go through the list of used strength_bits values in descending
541 * order.
542 */
543 for (i = max_strength_bits; i >= 0; i--)
544 if (number_uses[i] > 0)
545 ssl_cipher_apply_rule(0, 0, 0, 0, CIPHER_ORD, i,
546 list, head_p, tail_p);
547
548 Free(number_uses);
549 return(1);
550 }
435 551
436 if (ch == '\0') break; 552static int ssl_cipher_process_rulestr(const char *rule_str,
553 CIPHER_ORDER *list, CIPHER_ORDER **head_p,
554 CIPHER_ORDER **tail_p, SSL_CIPHER **ca_list)
555 {
556 unsigned long algorithms, mask, algo_strength, mask_strength;
557 const char *l, *start, *buf;
558 int j, multi, found, rule, retval, ok, buflen;
559 char ch;
560
561 retval = 1;
562 l = rule_str;
563 for (;;)
564 {
565 ch = *l;
437 566
567 if (ch == '\0')
568 break; /* done */
438 if (ch == '-') 569 if (ch == '-')
439 { j=CIPHER_DEL; l++; } 570 { rule = CIPHER_DEL; l++; }
440 else if (ch == '+') 571 else if (ch == '+')
441 { j=CIPHER_ORD; l++; } 572 { rule = CIPHER_ORD; l++; }
442 else if (ch == '!') 573 else if (ch == '!')
443 { j=CIPHER_KILL; l++; } 574 { rule = CIPHER_KILL; l++; }
444 else 575 else if (ch == '@')
445 { j=CIPHER_ADD; } 576 { rule = CIPHER_SPECIAL; l++; }
577 else
578 { rule = CIPHER_ADD; }
446 579
447 if (ITEM_SEP(ch)) 580 if (ITEM_SEP(ch))
448 { 581 {
449 l++; 582 l++;
450 continue; 583 continue;
451 } 584 }
452 ops[current_x].type=j; 585
453 ops[current_x].algorithms=0; 586 algorithms = mask = algo_strength = mask_strength = 0;
454 ops[current_x].mask=0;
455 587
456 start=l; 588 start=l;
457 for (;;) 589 for (;;)
458 { 590 {
459 ch= *l; 591 ch = *l;
460 i=0; 592 buf = l;
593 buflen = 0;
461#ifndef CHARSET_EBCDIC 594#ifndef CHARSET_EBCDIC
462 while ( ((ch >= 'A') && (ch <= 'Z')) || 595 while ( ((ch >= 'A') && (ch <= 'Z')) ||
463 ((ch >= '0') && (ch <= '9')) || 596 ((ch >= '0') && (ch <= '9')) ||
@@ -467,12 +600,28 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(SSL_METHOD *ssl_method,
467 while ( isalnum(ch) || (ch == '-')) 600 while ( isalnum(ch) || (ch == '-'))
468#endif 601#endif
469 { 602 {
470 buf[i]=ch; 603 ch = *(++l);
471 ch= *(++l); 604 buflen++;
472 i++;
473 if (i >= (CL_BUF-2)) break;
474 } 605 }
475 buf[i]='\0'; 606
607 if (buflen == 0)
608 {
609 /*
610 * We hit something, we cannot deal with,
611 * it is no command or separator nor
612 * alphanumeric, so we call this an error.
613 */
614 SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
615 SSL_R_INVALID_COMMAND);
616 retval = found = 0;
617 l++;
618 break;
619 }
620
621 if (rule == CIPHER_SPECIAL)
622 {
623 break; /* special treatment */
624 }
476 625
477 /* check for multi-part specification */ 626 /* check for multi-part specification */
478 if (ch == '+') 627 if (ch == '+')
@@ -483,133 +632,237 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(SSL_METHOD *ssl_method,
483 else 632 else
484 multi=0; 633 multi=0;
485 634
486 c_tmp.name=buf; 635 /*
487 j=sk_find(ca_list,(char *)&c_tmp); 636 * Now search for the name in the ca_list. Be careful
488 if (j < 0) 637 * with the strncmp, because the "buflen" limitation
489 goto end_loop; 638 * will make the rule "ADH:SOME" and the cipher
639 * "ADH-MY-CIPHER" look like a match for buflen=3.
640 * So additionally check, whether the cipher name found
641 * has the correct length. We can save a strlen() call,
642 * just checking for the '\0' at the right place is
643 * sufficient, we have to strncmp() anyway.
644 */
645 j = found = 0;
646 while (ca_list[j])
647 {
648 if ((ca_list[j]->name[buflen] == '\0') &&
649 !strncmp(buf, ca_list[j]->name, buflen))
650 {
651 found = 1;
652 break;
653 }
654 else
655 j++;
656 }
657 if (!found)
658 break; /* ignore this entry */
659
660 algorithms |= ca_list[j]->algorithms;
661 mask |= ca_list[j]->mask;
662 algo_strength |= ca_list[j]->algo_strength;
663 mask_strength |= ca_list[j]->mask_strength;
490 664
491 cp=(SSL_CIPHER *)sk_value(ca_list,j);
492 ops[current_x].algorithms|=cp->algorithms;
493 /* We add the SSL_SSL_MASK so we can match the
494 * SSLv2 and SSLv3 versions of RC4-MD5 */
495 ops[current_x].mask|=cp->mask;
496 if (!multi) break; 665 if (!multi) break;
497 } 666 }
498 current_x++; 667
499 if (ch == '\0') break; 668 /*
500end_loop: 669 * Ok, we have the rule, now apply it
501 /* Make sure we scan until the next valid start point */ 670 */
502 while ((*l != '\0') && ITEM_SEP(*l)) 671 if (rule == CIPHER_SPECIAL)
503 l++; 672 { /* special command */
673 ok = 0;
674 if ((buflen == 8) &&
675 !strncmp(buf, "STRENGTH", 8))
676 ok = ssl_cipher_strength_sort(list,
677 head_p, tail_p);
678 else
679 SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
680 SSL_R_INVALID_COMMAND);
681 if (ok == 0)
682 retval = 0;
683 /*
684 * We do not support any "multi" options
685 * together with "@", so throw away the
686 * rest of the command, if any left, until
687 * end or ':' is found.
688 */
689 while ((*l != '\0') && ITEM_SEP(*l))
690 l++;
691 }
692 else if (found)
693 {
694 ssl_cipher_apply_rule(algorithms, mask,
695 algo_strength, mask_strength, rule, -1,
696 list, head_p, tail_p);
697 }
698 else
699 {
700 while ((*l != '\0') && ITEM_SEP(*l))
701 l++;
702 }
703 if (*l == '\0') break; /* done */
504 } 704 }
505 705
506 num_x=current_x; 706 return(retval);
507 current_x=0; 707 }
508 708
509 /* We will now process the list of ciphers, once for each category, to 709STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
510 * decide what we should do with it. */ 710 STACK_OF(SSL_CIPHER) **cipher_list,
511 for (j=0; j<num_x; j++) 711 STACK_OF(SSL_CIPHER) **cipher_list_by_id,
712 const char *rule_str)
713 {
714 int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
715 unsigned long disabled_mask;
716 STACK_OF(SSL_CIPHER) *cipherstack;
717 const char *rule_p;
718 CIPHER_ORDER *list = NULL, *head = NULL, *tail = NULL, *curr;
719 SSL_CIPHER **ca_list = NULL;
720
721 /*
722 * Return with error if nothing to do.
723 */
724 if (rule_str == NULL) return(NULL);
725
726 if (init_ciphers) load_ciphers();
727
728 /*
729 * To reduce the work to do we only want to process the compiled
730 * in algorithms, so we first get the mask of disabled ciphers.
731 */
732 disabled_mask = ssl_cipher_get_disabled();
733
734 /*
735 * Now we have to collect the available ciphers from the compiled
736 * in ciphers. We cannot get more than the number compiled in, so
737 * it is used for allocation.
738 */
739 num_of_ciphers = ssl_method->num_ciphers();
740 list = (CIPHER_ORDER *)Malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
741 if (list == NULL)
512 { 742 {
513 algorithms=ops[j].algorithms; 743 SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
514 type=ops[j].type; 744 return(NULL); /* Failure */
515 mask=ops[j].mask; 745 }
516 746
517 curr=head; 747 ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, disabled_mask,
518 curr2=head; 748 list, &head, &tail);
519 tail2=tail; 749
520 for (;;) 750 /*
521 { 751 * We also need cipher aliases for selecting based on the rule_str.
522 if ((curr == NULL) || (curr == tail2)) break; 752 * There might be two types of entries in the rule_str: 1) names
523 curr=curr2; 753 * of ciphers themselves 2) aliases for groups of ciphers.
524 curr2=curr->next; 754 * For 1) we need the available ciphers and for 2) the cipher
755 * groups of cipher_aliases added together in one list (otherwise
756 * we would be happy with just the cipher_aliases table).
757 */
758 num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
759 num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
760 ca_list =
761 (SSL_CIPHER **)Malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
762 if (ca_list == NULL)
763 {
764 Free(list);
765 SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
766 return(NULL); /* Failure */
767 }
768 ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, disabled_mask,
769 head);
770
771 /*
772 * If the rule_string begins with DEFAULT, apply the default rule
773 * before using the (possibly available) additional rules.
774 */
775 ok = 1;
776 rule_p = rule_str;
777 if (strncmp(rule_str,"DEFAULT",7) == 0)
778 {
779 ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
780 list, &head, &tail, ca_list);
781 rule_p += 7;
782 if (*rule_p == ':')
783 rule_p++;
784 }
525 785
526 cp=curr->cipher; 786 if (ok && (strlen(rule_p) > 0))
527 ma=mask & cp->algorithms; 787 ok = ssl_cipher_process_rulestr(rule_p, list, &head, &tail,
528 if ((ma == 0) || ((ma & algorithms) != ma)) 788 ca_list);
529 {
530 /* does not apply */
531 continue;
532 }
533 789
534 /* add the cipher if it has not been added yet. */ 790 Free(ca_list); /* Not needed anymore */
535 if (type == CIPHER_ADD) 791
536 { 792 if (!ok)
537 if (!curr->active) 793 { /* Rule processing failure */
538 { 794 Free(list);
539 ll_append_tail(&head,curr,&tail); 795 return(NULL);
540 curr->active=1; 796 }
541 } 797 /*
542 } 798 * Allocate new "cipherstack" for the result, return with error
543 /* Move the added cipher to this location */ 799 * if we cannot get one.
544 else if (type == CIPHER_ORD) 800 */
545 { 801 if ((cipherstack = sk_SSL_CIPHER_new(NULL)) == NULL)
546 if (curr->active) 802 {
547 { 803 Free(list);
548 ll_append_tail(&head,curr,&tail); 804 return(NULL);
549 }
550 }
551 else if (type == CIPHER_DEL)
552 curr->active=0;
553 if (type == CIPHER_KILL)
554 {
555 if (head == curr)
556 head=curr->next;
557 else
558 curr->prev->next=curr->next;
559 if (tail == curr)
560 tail=curr->prev;
561 curr->active=0;
562 if (curr->next != NULL)
563 curr->next->prev=curr->prev;
564 if (curr->prev != NULL)
565 curr->prev->next=curr->next;
566 curr->next=NULL;
567 curr->prev=NULL;
568 }
569 }
570 } 805 }
571 806
572 for (curr=head; curr != NULL; curr=curr->next) 807 /*
808 * The cipher selection for the list is done. The ciphers are added
809 * to the resulting precedence to the STACK_OF(SSL_CIPHER).
810 */
811 for (curr = head; curr != NULL; curr = curr->next)
573 { 812 {
574 if (curr->active) 813 if (curr->active)
575 { 814 {
576 sk_SSL_CIPHER_push(ret,curr->cipher); 815 sk_SSL_CIPHER_push(cipherstack, curr->cipher);
577#ifdef CIPHER_DEBUG 816#ifdef CIPHER_DEBUG
578 printf("<%s>\n",curr->cipher->name); 817 printf("<%s>\n",curr->cipher->name);
579#endif 818#endif
580 } 819 }
581 } 820 }
582 821 Free(list); /* Not needed any longer */
822
823 /*
824 * The following passage is a little bit odd. If pointer variables
825 * were supplied to hold STACK_OF(SSL_CIPHER) return information,
826 * the old memory pointed to is free()ed. Then, however, the
827 * cipher_list entry will be assigned just a copy of the returned
828 * cipher stack. For cipher_list_by_id a copy of the cipher stack
829 * will be created. See next comment...
830 */
583 if (cipher_list != NULL) 831 if (cipher_list != NULL)
584 { 832 {
585 if (*cipher_list != NULL) 833 if (*cipher_list != NULL)
586 sk_SSL_CIPHER_free(*cipher_list); 834 sk_SSL_CIPHER_free(*cipher_list);
587 *cipher_list=ret; 835 *cipher_list = cipherstack;
588 } 836 }
589 837
590 if (cipher_list_by_id != NULL) 838 if (cipher_list_by_id != NULL)
591 { 839 {
592 if (*cipher_list_by_id != NULL) 840 if (*cipher_list_by_id != NULL)
593 sk_SSL_CIPHER_free(*cipher_list_by_id); 841 sk_SSL_CIPHER_free(*cipher_list_by_id);
594 *cipher_list_by_id=sk_SSL_CIPHER_dup(ret); 842 *cipher_list_by_id = sk_SSL_CIPHER_dup(cipherstack);
595 } 843 }
596 844
845 /*
846 * Now it is getting really strange. If something failed during
847 * the previous pointer assignment or if one of the pointers was
848 * not requested, the error condition is met. That might be
849 * discussable. The strange thing is however that in this case
850 * the memory "ret" pointed to is "free()ed" and hence the pointer
851 * cipher_list becomes wild. The memory reserved for
852 * cipher_list_by_id however is not "free()ed" and stays intact.
853 */
597 if ( (cipher_list_by_id == NULL) || 854 if ( (cipher_list_by_id == NULL) ||
598 (*cipher_list_by_id == NULL) || 855 (*cipher_list_by_id == NULL) ||
599 (cipher_list == NULL) || 856 (cipher_list == NULL) ||
600 (*cipher_list == NULL)) 857 (*cipher_list == NULL))
601 goto err; 858 {
859 sk_SSL_CIPHER_free(cipherstack);
860 return(NULL);
861 }
862
602 sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp); 863 sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);
603 864
604 ok=ret; 865 return(cipherstack);
605 ret=NULL;
606err:
607 if (tmp_str) Free(tmp_str);
608 if (ops != NULL) Free(ops);
609 if (ret != NULL) sk_SSL_CIPHER_free(ret);
610 if (ca_list != NULL) sk_free(ca_list);
611 if (list != NULL) Free(list);
612 return(ok);
613 } 866 }
614 867
615char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len) 868char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len)
@@ -617,15 +870,16 @@ char *SSL_CIPHER_description(SSL_CIPHER *cipher, char *buf, int len)
617 int is_export,pkl,kl; 870 int is_export,pkl,kl;
618 char *ver,*exp; 871 char *ver,*exp;
619 char *kx,*au,*enc,*mac; 872 char *kx,*au,*enc,*mac;
620 unsigned long alg,alg2; 873 unsigned long alg,alg2,alg_s;
621 static char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s\n"; 874 static char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s\n";
622 875
623 alg=cipher->algorithms; 876 alg=cipher->algorithms;
877 alg_s=cipher->algo_strength;
624 alg2=cipher->algorithm2; 878 alg2=cipher->algorithm2;
625 879
626 is_export=SSL_IS_EXPORT(alg); 880 is_export=SSL_C_IS_EXPORT(cipher);
627 pkl=SSL_EXPORT_PKEYLENGTH(alg); 881 pkl=SSL_C_EXPORT_PKEYLENGTH(cipher);
628 kl=SSL_EXPORT_KEYLENGTH(alg); 882 kl=SSL_C_EXPORT_KEYLENGTH(cipher);
629 exp=is_export?" export":""; 883 exp=is_export?" export":"";
630 884
631 if (alg & SSL_SSLV2) 885 if (alg & SSL_SSLV2)
@@ -752,37 +1006,16 @@ const char *SSL_CIPHER_get_name(SSL_CIPHER *c)
752 return("(NONE)"); 1006 return("(NONE)");
753 } 1007 }
754 1008
755/* number of bits for symetric cipher */ 1009/* number of bits for symmetric cipher */
756int SSL_CIPHER_get_bits(SSL_CIPHER *c, int *alg_bits) 1010int SSL_CIPHER_get_bits(SSL_CIPHER *c, int *alg_bits)
757 { 1011 {
758 int ret=0,a=0; 1012 int ret=0;
759 const EVP_CIPHER *enc;
760 const EVP_MD *md;
761 SSL_SESSION ss;
762 1013
763 if (c != NULL) 1014 if (c != NULL)
764 { 1015 {
765 ss.cipher=c; 1016 if (alg_bits != NULL) *alg_bits = c->alg_bits;
766 if (!ssl_cipher_get_evp(&ss,&enc,&md,NULL)) 1017 ret = c->strength_bits;
767 return(0);
768
769 a=EVP_CIPHER_key_length(enc)*8;
770
771 if (SSL_C_IS_EXPORT(c))
772 {
773 ret=SSL_C_EXPORT_KEYLENGTH(c)*8;
774 }
775 else
776 {
777 if (c->algorithm2 & SSL2_CF_8_BYTE_ENC)
778 ret=64;
779 else
780 ret=a;
781 }
782 } 1018 }
783
784 if (alg_bits != NULL) *alg_bits=a;
785
786 return(ret); 1019 return(ret);
787 } 1020 }
788 1021