summaryrefslogtreecommitdiff
path: root/sshbuf-misc.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2014-05-15 14:33:43 +1000
committerDamien Miller <djm@mindrot.org>2014-05-15 14:33:43 +1000
commit05e82c3b963c33048128baf72a6f6b3a1c10b4c1 (patch)
treecb238452459af2f8311d54ca509722497e799517 /sshbuf-misc.c
parent380948180f847a26f2d0c85b4dad3dca2ed2fd8b (diff)
- 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 @@
1/* $OpenBSD: sshbuf-misc.c,v 1.1 2014/04/30 05:29:56 djm Exp $ */
2/*
3 * Copyright (c) 2011 Damien Miller
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include "includes.h"
19
20#include <sys/types.h>
21#include <sys/socket.h>
22#include <netinet/in.h>
23#include <errno.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <limits.h>
27#include <string.h>
28#include <resolv.h>
29#include <ctype.h>
30
31#include "ssherr.h"
32#define SSHBUF_INTERNAL
33#include "sshbuf.h"
34
35void
36sshbuf_dump(struct sshbuf *buf, FILE *f)
37{
38 const u_char *p = sshbuf_ptr(buf);
39 size_t i, j, len = sshbuf_len(buf);
40
41 fprintf(f, "buffer %p len = %zu\n", buf, len);
42 for (i = 0; i < len; i += 16) {
43 fprintf(f, "%.4zd: ", i);
44 for (j = i; j < i + 16; j++) {
45 if (j < len)
46 fprintf(f, "%02x ", p[j]);
47 else
48 fprintf(f, " ");
49 }
50 fprintf(f, " ");
51 for (j = i; j < i + 16; j++) {
52 if (j < len) {
53 if (isascii(p[j]) && isprint(p[j]))
54 fprintf(f, "%c", p[j]);
55 else
56 fprintf(f, ".");
57 }
58 }
59 fprintf(f, "\n");
60 }
61}
62
63char *
64sshbuf_dtob16(struct sshbuf *buf)
65{
66 size_t i, j, len = sshbuf_len(buf);
67 const u_char *p = sshbuf_ptr(buf);
68 char *ret;
69 const char hex[] = "0123456789abcdef";
70
71 if (len == 0)
72 return strdup("");
73 if (SIZE_MAX / 2 <= len || (ret = malloc(len * 2 + 1)) == NULL)
74 return NULL;
75 for (i = j = 0; i < len; i++) {
76 ret[j++] = hex[(p[i] >> 4) & 0xf];
77 ret[j++] = hex[p[i] & 0xf];
78 }
79 ret[j] = '\0';
80 return ret;
81}
82
83char *
84sshbuf_dtob64(struct sshbuf *buf)
85{
86 size_t len = sshbuf_len(buf), plen;
87 const u_char *p = sshbuf_ptr(buf);
88 char *ret;
89 int r;
90
91 if (len == 0)
92 return strdup("");
93 plen = ((len + 2) / 3) * 4 + 1;
94 if (SIZE_MAX / 2 <= len || (ret = malloc(plen)) == NULL)
95 return NULL;
96 if ((r = b64_ntop(p, len, ret, plen)) == -1) {
97 bzero(ret, plen);
98 free(ret);
99 return NULL;
100 }
101 return ret;
102}
103
104int
105sshbuf_b64tod(struct sshbuf *buf, const char *b64)
106{
107 size_t plen = strlen(b64);
108 int nlen, r;
109 u_char *p;
110
111 if (plen == 0)
112 return 0;
113 if ((p = malloc(plen)) == NULL)
114 return SSH_ERR_ALLOC_FAIL;
115 if ((nlen = b64_pton(b64, p, plen)) < 0) {
116 bzero(p, plen);
117 free(p);
118 return SSH_ERR_INVALID_FORMAT;
119 }
120 if ((r = sshbuf_put(buf, p, nlen)) < 0) {
121 bzero(p, plen);
122 free(p);
123 return r;
124 }
125 bzero(p, plen);
126 free(p);
127 return 0;
128}
129