From 5eb094df1103cc7d527c1b6c280f5b5e259e8b69 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Fri, 23 Feb 2018 08:43:07 +0100 Subject: [PATCH] proposal: Print all algorithms even those with currently unknown transform types --- src/libstrongswan/crypto/proposal/proposal.c | 71 +++++++++++++------ .../tests/suites/test_proposal.c | 22 ++++++ 2 files changed, 72 insertions(+), 21 deletions(-) diff --git a/src/libstrongswan/crypto/proposal/proposal.c b/src/libstrongswan/crypto/proposal/proposal.c index be519c2569..7e5486577e 100644 --- a/src/libstrongswan/crypto/proposal/proposal.c +++ b/src/libstrongswan/crypto/proposal/proposal.c @@ -73,13 +73,31 @@ struct private_proposal_t { u_int number; }; +/** + * This is a hack to not change the previous order when printing proposals + */ +static transform_type_t type_for_sort(const void *type) +{ + const transform_type_t *t = type; + + switch (*t) + { + case PSEUDO_RANDOM_FUNCTION: + return INTEGRITY_ALGORITHM; + case INTEGRITY_ALGORITHM: + return PSEUDO_RANDOM_FUNCTION; + default: + return *t; + } +} + /** * Sort transform types */ static int type_sort(const void *a, const void *b, void *user) { - const transform_type_t *ta = a, *tb = b; - return *ta - *tb; + transform_type_t ta = type_for_sort(a), tb = type_for_sort(b); + return ta - tb; } /** @@ -724,30 +742,44 @@ static bool add_string_algo(private_proposal_t *this, const char *alg) } /** - * print all algorithms of a kind to buffer + * Print all algorithms of the given type */ static int print_alg(private_proposal_t *this, printf_hook_data_t *data, - u_int kind, void *names, bool *first) + transform_type_t type, bool *first) { enumerator_t *enumerator; size_t written = 0; - uint16_t alg, size; + entry_t *entry; + enum_name_t *names; - enumerator = create_enumerator(this, kind); - while (enumerator->enumerate(enumerator, &alg, &size)) + names = transform_get_enum_names(type); + + enumerator = array_create_enumerator(this->transforms); + while (enumerator->enumerate(enumerator, &entry)) { + char *prefix = "/"; + + if (type != entry->type) + { + continue; + } if (*first) { - written += print_in_hook(data, "%N", names, alg); + prefix = ""; *first = FALSE; } + if (names) + { + written += print_in_hook(data, "%s%N", prefix, names, entry->alg); + } else { - written += print_in_hook(data, "/%N", names, alg); + written += print_in_hook(data, "%sUNKNOWN_%u_%u", prefix, + entry->type, entry->alg); } - if (size) + if (entry->key_size) { - written += print_in_hook(data, "_%u", size); + written += print_in_hook(data, "_%u", entry->key_size); } } enumerator->destroy(enumerator); @@ -763,6 +795,7 @@ int proposal_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, private_proposal_t *this = *((private_proposal_t**)(args[0])); linked_list_t *list = *((linked_list_t**)(args[0])); enumerator_t *enumerator; + transform_type_t *type; size_t written = 0; bool first = TRUE; @@ -791,16 +824,12 @@ int proposal_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, } written = print_in_hook(data, "%N:", protocol_id_names, this->protocol); - written += print_alg(this, data, ENCRYPTION_ALGORITHM, - encryption_algorithm_names, &first); - written += print_alg(this, data, INTEGRITY_ALGORITHM, - integrity_algorithm_names, &first); - written += print_alg(this, data, PSEUDO_RANDOM_FUNCTION, - pseudo_random_function_names, &first); - written += print_alg(this, data, DIFFIE_HELLMAN_GROUP, - diffie_hellman_group_names, &first); - written += print_alg(this, data, EXTENDED_SEQUENCE_NUMBERS, - extended_sequence_numbers_names, &first); + enumerator = array_create_enumerator(this->types); + while (enumerator->enumerate(enumerator, &type)) + { + written += print_alg(this, data, *type, &first); + } + enumerator->destroy(enumerator); return written; } diff --git a/src/libstrongswan/tests/suites/test_proposal.c b/src/libstrongswan/tests/suites/test_proposal.c index 1a2f97d5fd..947c321592 100644 --- a/src/libstrongswan/tests/suites/test_proposal.c +++ b/src/libstrongswan/tests/suites/test_proposal.c @@ -194,6 +194,24 @@ START_TEST(test_promote_dh_group_not_contained) } END_TEST +START_TEST(test_unknown_transform_types_print) +{ + proposal_t *proposal; + + proposal = proposal_create(PROTO_IKE, 0); + proposal->add_algorithm(proposal, 242, 42, 128); + assert_proposal_eq(proposal, "IKE:UNKNOWN_242_42_128"); + proposal->destroy(proposal); + + proposal = proposal_create_from_string(PROTO_IKE, + "aes128-sha256-modp3072-ecp256"); + proposal->add_algorithm(proposal, 242, 42, 128); + proposal->add_algorithm(proposal, 243, 1, 0); + assert_proposal_eq(proposal, "IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_3072/ECP_256/UNKNOWN_242_42_128/UNKNOWN_243_1"); + proposal->destroy(proposal); +} +END_TEST + Suite *proposal_suite_create() { Suite *s; @@ -216,5 +234,9 @@ Suite *proposal_suite_create() tcase_add_test(tc, test_promote_dh_group_not_contained); suite_add_tcase(s, tc); + tc = tcase_create("unknown transform types"); + tcase_add_test(tc, test_unknown_transform_types_print); + suite_add_tcase(s, tc); + return s; } -- 2.39.2