aboutsummaryrefslogtreecommitdiff
path: root/sshbuf-misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'sshbuf-misc.c')
-rw-r--r--sshbuf-misc.c63
1 files changed, 57 insertions, 6 deletions
diff --git a/sshbuf-misc.c b/sshbuf-misc.c
index 9c5c42bba70a..7b11e4e1760b 100644
--- a/sshbuf-misc.c
+++ b/sshbuf-misc.c
@@ -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.22 2025/09/04 00:32:31 djm Exp $ */
/*
* Copyright (c) 2011 Damien Miller
*
@@ -22,9 +22,7 @@
#include <netinet/in.h>
#include <errno.h>
#include <stdlib.h>
-#ifdef HAVE_STDINT_H
-# include <stdint.h>
-#endif
+#include <stdint.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
@@ -71,7 +69,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 +88,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)
{
@@ -176,6 +210,9 @@ sshbuf_dtourlb64(const struct sshbuf *d, struct sshbuf *b64, int wrap)
struct sshbuf *b = NULL;
size_t i, l;
+ if (sshbuf_len(d) == 0)
+ return 0;
+
if ((b = sshbuf_new()) == NULL)
return SSH_ERR_ALLOC_FAIL;
/* Encode using regular base64; we'll transform it once done */
@@ -218,7 +255,7 @@ sshbuf_dup_string(struct sshbuf *buf)
size_t l = sshbuf_len(buf);
char *r;
- if (s == NULL || l > SIZE_MAX)
+ if (s == NULL || l >= SIZE_MAX)
return NULL;
/* accept a nul only as the last character in the buffer */
if (l > 0 && (p = memchr(s, '\0', l)) != NULL) {
@@ -250,6 +287,20 @@ sshbuf_cmp(const struct sshbuf *b, size_t offset,
}
int
+sshbuf_equals(const struct sshbuf *a, const struct sshbuf *b)
+{
+ if (sshbuf_ptr(a) == NULL || sshbuf_ptr(b) == NULL)
+ return SSH_ERR_INTERNAL_ERROR;
+ if (sshbuf_len(a) != sshbuf_len(b))
+ return SSH_ERR_MESSAGE_INCOMPLETE;
+ if (sshbuf_len(a) == 0)
+ return 0;
+ if (memcmp(sshbuf_ptr(a), sshbuf_ptr(b), sshbuf_len(a)) != 0)
+ return SSH_ERR_INVALID_FORMAT;
+ return 0;
+}
+
+int
sshbuf_find(const struct sshbuf *b, size_t start_offset,
const void *s, size_t len, size_t *offsetp)
{