From f2e7086902b3605c96e54ef9c956ca7ab000010e Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 18 Nov 2023 08:50:58 +0000 Subject: [PATCH] Factor out the check for ext-info-* keyword. I'm about to want to use the same code to check for something else. It's only a handful of lines, but even so. Also, since the string constants are mentioned several times, this seems like a good moment to lift them out into reusable static const ptrlens. --- ssh/transport2.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/ssh/transport2.c b/ssh/transport2.c index 3fe358f8..69346b4f 100644 --- a/ssh/transport2.c +++ b/ssh/transport2.c @@ -27,6 +27,9 @@ const static ssh2_macalg *const buggymacs[] = { &ssh_hmac_sha1_buggy, &ssh_hmac_sha1_96_buggy, &ssh_hmac_md5 }; +const static ptrlen ext_info_c = PTRLEN_DECL_LITERAL("ext-info-c"); +const static ptrlen ext_info_s = PTRLEN_DECL_LITERAL("ext-info-s"); + static ssh_compressor *ssh_comp_none_init(void) { return NULL; @@ -938,9 +941,9 @@ static void ssh2_write_kexinit_lists( } if (i == KEXLIST_KEX && first_time) { if (our_hostkeys) /* we're the server */ - add_to_commasep(list, "ext-info-s"); + add_to_commasep_pl(list, ext_info_s); else /* we're the client */ - add_to_commasep(list, "ext-info-c"); + add_to_commasep_pl(list, ext_info_c); } put_stringsb(pktout, list); } @@ -955,6 +958,14 @@ struct server_hostkeys { size_t n, size; }; +static bool kexinit_keyword_found(ptrlen list, ptrlen keyword) +{ + for (ptrlen word; get_commasep_word(&list, &word) ;) + if (ptrlen_eq_ptrlen(word, keyword)) + return true; + return false; +} + static bool ssh2_scan_kexinits( ptrlen client_kexinit, ptrlen server_kexinit, bool we_are_server, struct kexinit_algorithm_list kexlists[NKEXLIST], @@ -1165,16 +1176,10 @@ static bool ssh2_scan_kexinits( /* * Check whether the other side advertised support for EXT_INFO. */ - { - ptrlen extinfo_advert = - (we_are_server ? PTRLEN_LITERAL("ext-info-c") : - PTRLEN_LITERAL("ext-info-s")); - ptrlen list = (we_are_server ? clists[KEXLIST_KEX] : - slists[KEXLIST_KEX]); - for (ptrlen word; get_commasep_word(&list, &word) ;) - if (ptrlen_eq_ptrlen(word, extinfo_advert)) - *can_send_ext_info = true; - } + if (kexinit_keyword_found( + we_are_server ? clists[KEXLIST_KEX] : slists[KEXLIST_KEX], + we_are_server ? ext_info_c : ext_info_s)) + *can_send_ext_info = true; if (server_hostkeys) { /*