summaryrefslogtreecommitdiffstats
path: root/sshbuf-misc.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2014-05-15 06:33:43 +0200
committerDamien Miller <djm@mindrot.org>2014-05-15 06:33:43 +0200
commit05e82c3b963c33048128baf72a6f6b3a1c10b4c1 (patch)
treecb238452459af2f8311d54ca509722497e799517 /sshbuf-misc.c
parent - dtucker@cvs.openbsd.org 2014/04/29 20:36:51 (diff)
downloadopenssh-05e82c3b963c33048128baf72a6f6b3a1c10b4c1.tar.xz
openssh-05e82c3b963c33048128baf72a6f6b3a1c10b4c1.zip
- djm@cvs.openbsd.org 2014/04/30 05:29:56
[bufaux.c bufbn.c bufec.c buffer.c buffer.h sshbuf-getput-basic.c] [sshbuf-getput-crypto.c sshbuf-misc.c sshbuf.c sshbuf.h ssherr.c] [ssherr.h] New buffer API; the first installment of the conversion/replacement of OpenSSH's internals to make them usable as a standalone library. This includes a set of wrappers to make it compatible with the existing buffer API so replacement can occur incrementally. With and ok markus@ Thanks also to Ben Hawkes, David Tomaschik, Ivan Fratric, Matthew Dempsky and Ron Bowes for a detailed review.
Diffstat (limited to 'sshbuf-misc.c')
-rw-r--r--sshbuf-misc.c129
1 files changed, 129 insertions, 0 deletions
diff --git a/sshbuf-misc.c b/sshbuf-misc.c
new file mode 100644
index 000000000..22dbfd5a4
--- /dev/null
+++ b/sshbuf-misc.c
@@ -0,0 +1,129 @@
+/* $OpenBSD: sshbuf-misc.c,v 1.1 2014/04/30 05:29:56 djm Exp $ */
+/*
+ * Copyright (c) 2011 Damien Miller
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "includes.h"
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <limits.h>
+#include <string.h>
+#include <resolv.h>
+#include <ctype.h>
+
+#include "ssherr.h"
+#define SSHBUF_INTERNAL
+#include "sshbuf.h"
+
+void
+sshbuf_dump(struct sshbuf *buf, FILE *f)
+{
+ const u_char *p = sshbuf_ptr(buf);
+ size_t i, j, len = sshbuf_len(buf);
+
+ fprintf(f, "buffer %p len = %zu\n", buf, len);
+ for (i = 0; i < len; i += 16) {
+ fprintf(f, "%.4zd: ", i);
+ for (j = i; j < i + 16; j++) {
+ if (j < len)
+ fprintf(f, "%02x ", p[j]);
+ else
+ fprintf(f, " ");
+ }
+ fprintf(f, " ");
+ for (j = i; j < i + 16; j++) {
+ if (j < len) {
+ if (isascii(p[j]) && isprint(p[j]))
+ fprintf(f, "%c", p[j]);
+ else
+ fprintf(f, ".");
+ }
+ }
+ fprintf(f, "\n");
+ }
+}
+
+char *
+sshbuf_dtob16(struct sshbuf *buf)
+{
+ size_t i, j, len = sshbuf_len(buf);
+ const u_char *p = sshbuf_ptr(buf);
+ char *ret;
+ const char hex[] = "0123456789abcdef";
+
+ if (len == 0)
+ return strdup("");
+ if (SIZE_MAX / 2 <= len || (ret = malloc(len * 2 + 1)) == NULL)
+ return NULL;
+ for (i = j = 0; i < len; i++) {
+ ret[j++] = hex[(p[i] >> 4) & 0xf];
+ ret[j++] = hex[p[i] & 0xf];
+ }
+ ret[j] = '\0';
+ return ret;
+}
+
+char *
+sshbuf_dtob64(struct sshbuf *buf)
+{
+ size_t len = sshbuf_len(buf), plen;
+ const u_char *p = sshbuf_ptr(buf);
+ char *ret;
+ int r;
+
+ if (len == 0)
+ return strdup("");
+ plen = ((len + 2) / 3) * 4 + 1;
+ if (SIZE_MAX / 2 <= len || (ret = malloc(plen)) == NULL)
+ return NULL;
+ if ((r = b64_ntop(p, len, ret, plen)) == -1) {
+ bzero(ret, plen);
+ free(ret);
+ return NULL;
+ }
+ return ret;
+}
+
+int
+sshbuf_b64tod(struct sshbuf *buf, const char *b64)
+{
+ size_t plen = strlen(b64);
+ int nlen, r;
+ u_char *p;
+
+ if (plen == 0)
+ return 0;
+ if ((p = malloc(plen)) == NULL)
+ return SSH_ERR_ALLOC_FAIL;
+ if ((nlen = b64_pton(b64, p, plen)) < 0) {
+ bzero(p, plen);
+ free(p);
+ return SSH_ERR_INVALID_FORMAT;
+ }
+ if ((r = sshbuf_put(buf, p, nlen)) < 0) {
+ bzero(p, plen);
+ free(p);
+ return r;
+ }
+ bzero(p, plen);
+ free(p);
+ return 0;
+}
+