]> git.kaiwu.me - haproxy.git/commitdiff
MINOR: ssl: add alpn_remove_token() to drop a protocol from an ALPN string
authorWilly Tarreau <w@1wt.eu>
Mon, 12 Oct 2026 18:31:19 +0000 (18:31 +0000)
committerWilly Tarreau <w@1wt.eu>
Tue, 17 Nov 2026 10:08:46 +0000 (11:08 +0100)
This function removes all occurrences of a given protocol name from an
ALPN string in the binary form produced by ssl_sock_parse_alpn(), in
place, and updates its length. The string remains zero-terminated so that
the existing strstr() checks on it keep working, and it may end up empty,
which is then equivalent to "no-alpn". It returns the number of entries
removed.

It will be used by future patches to globally disable some protocols from
frontend ALPN strings at boot.

include/haproxy/ssl_utils.h
src/ssl_utils.c

index 1d1b169664b0eabf96e75684c91a194b17da1151..392436c8fb83dbb6faf26319867901f2b176da53 100644 (file)
@@ -47,6 +47,7 @@ const char *x509_v_err_int_to_str(int code);
 long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d);
 const char *x509_get_notbefore(X509 *cert);
 const char *x509_get_notafter(X509 *cert);
+int alpn_remove_token(char *alpn, int *len, const struct ist token);
 #ifdef HAVE_ASN1_TIME_TO_TM
 time_t ASN1_to_time_t(ASN1_TIME *asn1_time);
 time_t x509_get_notafter_time_t(X509 *cert);
index 431b5d47c6b456d977fca9a6f80a7a1fea101601..ef5e6fc0cf0a169103f79468599d4afc11c92e92 100644 (file)
@@ -842,6 +842,38 @@ end:
        return NULL;
 }
 
+/* Removes all occurrences of protocol <token> from the ALPN string <alpn>
+ * whose length is <*len>, in the binary format produced by
+ * ssl_sock_parse_alpn() (each name preceded by its length byte). The string
+ * is modified in place, remains zero-terminated and <*len> is updated. It may
+ * end up empty, which is then equivalent to "no-alpn". Returns the number of
+ * entries removed.
+ */
+int alpn_remove_token(char *alpn, int *len, const struct ist token)
+{
+       int pos = 0, removed = 0;
+       int next;
+
+       while (pos < *len) {
+               next = pos + 1 + (uchar)alpn[pos];
+
+               /* truncated entry, should never happen */
+               if (next > *len)
+                       break;
+
+               if ((uchar)alpn[pos] == istlen(token) &&
+                   memcmp(alpn + pos + 1, istptr(token), istlen(token)) == 0) {
+                       memmove(alpn + pos, alpn + next, *len - next);
+                       *len -= next - pos;
+                       alpn[*len] = '\0';
+                       removed++;
+               }
+               else
+                       pos = next;
+       }
+       return removed;
+}
+
 #ifdef HAVE_ASN1_TIME_TO_TM
 /* Takes a ASN1_TIME and converts it into a time_t */
 time_t ASN1_to_time_t(ASN1_TIME *asn1_time)