]> git.ipfire.org Git - thirdparty/openssh-portable.git/commitdiff
upstream: add a sshbuf_get_nulterminated_string() function to pull a
authordjm@openbsd.org <djm@openbsd.org>
Fri, 21 Nov 2025 01:29:06 +0000 (01:29 +0000)
committerDamien Miller <djm@mindrot.org>
Fri, 21 Nov 2025 01:56:27 +0000 (12:56 +1100)
\0- terminated string from a sshbuf. Intended to be used to improve parsing
of SOCKS headers for dynamic forwarding.

ok deraadt; feedback Tim van der Molen

OpenBSD-Commit-ID: cf93d6db4730f7518d5269c279e16b172b484b36

sshbuf-getput-basic.c
sshbuf.h

index 2cc562b244f2fac4cc31a67f926a98be43701f1d..405f7eb60e0299117b4e48f10705e940b7e5e4f4 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: sshbuf-getput-basic.c,v 1.13 2022/05/25 06:03:44 djm Exp $    */
+/*     $OpenBSD: sshbuf-getput-basic.c,v 1.14 2025/11/21 01:29:06 djm Exp $    */
 /*
  * Copyright (c) 2011 Damien Miller
  *
@@ -629,3 +629,41 @@ sshbuf_get_bignum2_bytes_direct(struct sshbuf *buf,
        }
        return 0;
 }
+
+int
+sshbuf_get_nulterminated_string(struct sshbuf *buf, size_t maxlen,
+    char **valp, size_t *lenp)
+{
+       const u_char zero = 0;
+       char *val = NULL;
+       size_t len = 0;
+       int r;
+
+       if (valp != NULL)
+               *valp = NULL;
+       if (lenp != NULL)
+               *lenp = 0;
+       if ((r = sshbuf_find(buf, 0, &zero, sizeof(zero), &len)) != 0) {
+               if (r == SSH_ERR_INVALID_FORMAT && sshbuf_len(buf) < maxlen)
+                       return SSH_ERR_MESSAGE_INCOMPLETE;
+               return r;
+       }
+       if (len > maxlen)
+               return SSH_ERR_INVALID_FORMAT;
+       /* can strdup() because it's definitely nul-terminated */
+       if ((val = strdup(sshbuf_ptr(buf))) == NULL)
+               return SSH_ERR_ALLOC_FAIL;
+       if ((r = sshbuf_consume(buf, len + 1)) != 0)
+               goto out;
+       /* success */
+       r = 0;
+       if (valp != NULL) {
+               *valp = val;
+               val = NULL;
+       }
+       if (lenp != NULL)
+               *lenp = len;
+ out:
+       free(val);
+       return r;
+}
index 0c82f120c4226a8277a5734d30c37a567b94eec5..8c18ded02fd029f279b551959c96486f3bb4f6b7 100644 (file)
--- a/sshbuf.h
+++ b/sshbuf.h
@@ -1,4 +1,4 @@
-/*     $OpenBSD: sshbuf.h,v 1.32 2025/09/02 09:41:23 djm Exp $ */
+/*     $OpenBSD: sshbuf.h,v 1.33 2025/11/21 01:29:06 djm Exp $ */
 /*
  * Copyright (c) 2011 Damien Miller
  *
@@ -229,6 +229,10 @@ int        sshbuf_put_ec_pkey(struct sshbuf *buf, EVP_PKEY *pkey);
 # endif /* OPENSSL_HAS_ECC */
 #endif /* WITH_OPENSSL */
 
+/* Functions to extract or store various non-SSH wire encoded values */
+int    sshbuf_get_nulterminated_string(struct sshbuf *buf, size_t maxlen,
+           char **valp, size_t *lenp);
+
 /* Dump the contents of the buffer in a human-readable format */
 void   sshbuf_dump(const struct sshbuf *buf, FILE *f);