summaryrefslogtreecommitdiff
path: root/sshbuf-misc.c
diff options
context:
space:
mode:
authordjm@openbsd.org <djm@openbsd.org>2019-07-16 13:18:39 +0000
committerDamien Miller <djm@mindrot.org>2019-07-16 23:23:05 +1000
commit16dd8b2c78a0de106c7429e2a294d203f6bda3c7 (patch)
treec1f6e71fe7aa7985f055b6a66926def4c876dcb7 /sshbuf-misc.c
parent45478898f9590b5cc8bc7104e573b84be67443b0 (diff)
upstream: remove mostly vestigal uuencode.[ch]; moving the only unique
functionality there (wrapping of base64-encoded data) to sshbuf functions; feedback and ok markus@ OpenBSD-Commit-ID: 4dba6735d88c57232f6fccec8a08bdcfea44ac4c
Diffstat (limited to 'sshbuf-misc.c')
-rw-r--r--sshbuf-misc.c57
1 files changed, 46 insertions, 11 deletions
diff --git a/sshbuf-misc.c b/sshbuf-misc.c
index 83b598103..4a4985fd3 100644
--- a/sshbuf-misc.c
+++ b/sshbuf-misc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: sshbuf-misc.c,v 1.8 2019/07/15 13:11:38 djm Exp $ */ 1/* $OpenBSD: sshbuf-misc.c,v 1.9 2019/07/16 13:18:39 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2011 Damien Miller 3 * Copyright (c) 2011 Damien Miller
4 * 4 *
@@ -89,23 +89,58 @@ sshbuf_dtob16(struct sshbuf *buf)
89 return ret; 89 return ret;
90} 90}
91 91
92int
93sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap)
94{
95 size_t i, slen = 0;
96 char *s = NULL;
97 int r;
98
99 if (d == NULL || b64 == NULL || sshbuf_len(d) >= SIZE_MAX / 2)
100 return SSH_ERR_INVALID_ARGUMENT;
101 if (sshbuf_len(d) == 0)
102 return 0;
103 slen = ((sshbuf_len(d) + 2) / 3) * 4 + 1;
104 if ((s = malloc(slen)) == NULL)
105 return SSH_ERR_ALLOC_FAIL;
106 if (b64_ntop(sshbuf_ptr(d), sshbuf_len(d), s, slen) == -1) {
107 r = SSH_ERR_INTERNAL_ERROR;
108 goto fail;
109 }
110 if (wrap) {
111 for (i = 0; s[i] != '\0'; i++) {
112 if ((r = sshbuf_put_u8(b64, s[i])) != 0)
113 goto fail;
114 if (i % 70 == 69 && (r = sshbuf_put_u8(b64, '\n')) != 0)
115 goto fail;
116 }
117 if (i % 70 != 69 && (r = sshbuf_put_u8(b64, '\n')) != 0)
118 goto fail;
119 } else {
120 if ((r = sshbuf_put(b64, s, strlen(s))) != 0)
121 goto fail;
122 }
123 /* Success */
124 r = 0;
125 fail:
126 freezero(s, slen);
127 return r;
128}
129
92char * 130char *
93sshbuf_dtob64(struct sshbuf *buf) 131sshbuf_dtob64_string(const struct sshbuf *buf, int wrap)
94{ 132{
95 size_t len = sshbuf_len(buf), plen; 133 struct sshbuf *tmp;
96 const u_char *p = sshbuf_ptr(buf);
97 char *ret; 134 char *ret;
98 135
99 if (len == 0) 136 if ((tmp = sshbuf_new()) == NULL)
100 return strdup("");
101 plen = ((len + 2) / 3) * 4 + 1;
102 if (SIZE_MAX / 2 <= len || (ret = malloc(plen)) == NULL)
103 return NULL; 137 return NULL;
104 if (b64_ntop(p, len, ret, plen) == -1) { 138 if (sshbuf_dtob64(buf, tmp, wrap) != 0) {
105 explicit_bzero(ret, plen); 139 sshbuf_free(tmp);
106 free(ret);
107 return NULL; 140 return NULL;
108 } 141 }
142 ret = sshbuf_dup_string(tmp);
143 sshbuf_free(tmp);
109 return ret; 144 return ret;
110} 145}
111 146