xref: /openssh-portable/sshbuf-misc.c (revision 05e82c3b)
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 
35 void
36 sshbuf_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 
63 char *
64 sshbuf_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 
83 char *
84 sshbuf_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 
104 int
105 sshbuf_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 
130