]> git.ipfire.org Git - thirdparty/openssh-portable.git/commitdiff
upstream: function to make a sshbuf from a hex string; useful in
authordjm@openbsd.org <djm@openbsd.org>
Wed, 21 May 2025 06:43:48 +0000 (06:43 +0000)
committerDamien Miller <djm@mindrot.org>
Wed, 21 May 2025 08:49:11 +0000 (18:49 +1000)
tests

also constify some arguments

OpenBSD-Commit-ID: 00f9c25b256be0efd73f2d8268ff041bc45ffb2c

sshbuf-misc.c
sshbuf.h

index 9c5c42bba70ac1042c080da1744ddeb2274a6dfb..adbf9903b4d89875c3801304409d5fd89e2d5e13 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: sshbuf-misc.c,v 1.18 2022/01/22 00:43:43 djm Exp $    */
+/*     $OpenBSD: sshbuf-misc.c,v 1.19 2025/05/21 06:43:48 djm Exp $    */
 /*
  * Copyright (c) 2011 Damien Miller
  *
@@ -71,7 +71,7 @@ sshbuf_dump(const struct sshbuf *buf, FILE *f)
 }
 
 char *
-sshbuf_dtob16(struct sshbuf *buf)
+sshbuf_dtob16(const struct sshbuf *buf)
 {
        size_t i, j, len = sshbuf_len(buf);
        const u_char *p = sshbuf_ptr(buf);
@@ -90,6 +90,42 @@ sshbuf_dtob16(struct sshbuf *buf)
        return ret;
 }
 
+static int
+b16tod(const char v)
+{
+       if (v >= '0' && v <= '9')
+               return v - '0';
+       if (v >= 'a' && v <= 'f')
+               return 10 + v - 'a';
+       if (v >= 'A' && v <= 'A')
+               return 10 + v - 'A';
+       return -1;
+}
+
+struct sshbuf *
+sshbuf_b16tod(const char *b16)
+{
+       struct sshbuf *ret;
+       size_t o;
+       int r, v1, v2;
+
+       if ((ret = sshbuf_new()) == NULL)
+               return NULL;
+       for (o = 0; b16[o] != '\0'; o += 2) {
+               if ((v1 = b16tod(b16[o])) == -1 ||
+                   (v2 = b16tod(b16[o + 1])) == -1) {
+                       sshbuf_free(ret);
+                       return NULL;
+               }
+               if ((r = sshbuf_put_u8(ret, (u_char)((v1 << 4) | v2))) != 0) {
+                       sshbuf_free(ret);
+                       return NULL;
+               }
+       }
+       /* success */
+       return ret;
+}
+
 int
 sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap)
 {
index 49c32af8fbb794b50999341b5301324e860dfa4b..681abb9eec03e0face34c023d00be4f39352b333 100644 (file)
--- a/sshbuf.h
+++ b/sshbuf.h
@@ -1,4 +1,4 @@
-/*     $OpenBSD: sshbuf.h,v 1.29 2024/08/15 00:51:51 djm Exp $ */
+/*     $OpenBSD: sshbuf.h,v 1.30 2025/05/21 06:43:48 djm Exp $ */
 /*
  * Copyright (c) 2011 Damien Miller
  *
@@ -235,7 +235,9 @@ void        sshbuf_dump(const struct sshbuf *buf, FILE *f);
 void   sshbuf_dump_data(const void *s, size_t len, FILE *f);
 
 /* Return the hexadecimal representation of the contents of the buffer */
-char   *sshbuf_dtob16(struct sshbuf *buf);
+char   *sshbuf_dtob16(const struct sshbuf *buf);
+/* Make a sshbuf from a hex string */
+struct sshbuf *sshbuf_b16tod(const char *b16);
 
 /* Encode the contents of the buffer as base64 */
 char   *sshbuf_dtob64_string(const struct sshbuf *buf, int wrap);