From 965324d150b471ac3c0d2104d12247e0b4979de9 Mon Sep 17 00:00:00 2001
From: Simon Tatham <anakin@pobox.com>
Date: Sat, 18 Nov 2023 08:50:58 +0000
Subject: 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.

Origin: upstream, https://git.tartarus.org/?p=simon/putty.git;a=commit;h=f2e7086902b3605c96e54ef9c956ca7ab000010e
Last-Update: 2023-12-18

Patch-Name: refactor-ext-info-s-check.patch
---
 ssh/transport2.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)

diff --git a/ssh/transport2.c b/ssh/transport2.c
index bbad06e3..42fcb34e 100644
--- a/ssh/transport2.c
+++ b/ssh/transport2.c
@@ -26,6 +26,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;
@@ -935,9 +938,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);
     }
@@ -952,6 +955,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],
@@ -1162,16 +1173,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) {
         /*
