summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile.in2
-rw-r--r--auth2-pubkey.c4
-rw-r--r--ssh-keygen.c53
-rw-r--r--sshbuf-misc.c57
-rw-r--r--sshbuf.h5
-rw-r--r--sshkey.c27
-rw-r--r--uuencode.c95
-rw-r--r--uuencode.h29
8 files changed, 84 insertions, 188 deletions
diff --git a/Makefile.in b/Makefile.in
index 8b6754a68..92ba0b37e 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -90,7 +90,7 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
90 compat.o fatal.o hostfile.o \ 90 compat.o fatal.o hostfile.o \
91 log.o match.o moduli.o nchan.o packet.o \ 91 log.o match.o moduli.o nchan.o packet.o \
92 readpass.o ttymodes.o xmalloc.o addrmatch.o \ 92 readpass.o ttymodes.o xmalloc.o addrmatch.o \
93 atomicio.o dispatch.o mac.o uuencode.o misc.o utf8.o \ 93 atomicio.o dispatch.o mac.o misc.o utf8.o \
94 monitor_fdpass.o rijndael.o ssh-dss.o ssh-ecdsa.o ssh-rsa.o dh.o \ 94 monitor_fdpass.o rijndael.o ssh-dss.o ssh-ecdsa.o ssh-rsa.o dh.o \
95 msg.o progressmeter.o dns.o entropy.o gss-genr.o umac.o umac128.o \ 95 msg.o progressmeter.o dns.o entropy.o gss-genr.o umac.o umac128.o \
96 ssh-pkcs11.o smult_curve25519_ref.o \ 96 ssh-pkcs11.o smult_curve25519_ref.o \
diff --git a/auth2-pubkey.c b/auth2-pubkey.c
index 05fa5c9f8..32ce19250 100644
--- a/auth2-pubkey.c
+++ b/auth2-pubkey.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: auth2-pubkey.c,v 1.90 2019/06/21 03:19:59 djm Exp $ */ 1/* $OpenBSD: auth2-pubkey.c,v 1.91 2019/07/16 13:18:39 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved. 3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * 4 *
@@ -109,7 +109,7 @@ userauth_pubkey(struct ssh *ssh)
109 109
110 if ((pkbuf = sshbuf_from(pkblob, blen)) == NULL) 110 if ((pkbuf = sshbuf_from(pkblob, blen)) == NULL)
111 fatal("%s: sshbuf_from failed", __func__); 111 fatal("%s: sshbuf_from failed", __func__);
112 if ((keystring = sshbuf_dtob64(pkbuf)) == NULL) 112 if ((keystring = sshbuf_dtob64_string(pkbuf, 0)) == NULL)
113 fatal("%s: sshbuf_dtob64 failed", __func__); 113 fatal("%s: sshbuf_dtob64 failed", __func__);
114 debug2("%s: %s user %s %s public key %s %s", __func__, 114 debug2("%s: %s user %s %s public key %s %s", __func__,
115 authctxt->valid ? "valid" : "invalid", authctxt->user, 115 authctxt->valid ? "valid" : "invalid", authctxt->user,
diff --git a/ssh-keygen.c b/ssh-keygen.c
index 5dcad1f61..c2cfe8df0 100644
--- a/ssh-keygen.c
+++ b/ssh-keygen.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssh-keygen.c,v 1.336 2019/07/15 13:16:29 djm Exp $ */ 1/* $OpenBSD: ssh-keygen.c,v 1.337 2019/07/16 13:18:39 djm Exp $ */
2/* 2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -43,7 +43,6 @@
43#include "xmalloc.h" 43#include "xmalloc.h"
44#include "sshkey.h" 44#include "sshkey.h"
45#include "authfile.h" 45#include "authfile.h"
46#include "uuencode.h"
47#include "sshbuf.h" 46#include "sshbuf.h"
48#include "pathnames.h" 47#include "pathnames.h"
49#include "log.h" 48#include "log.h"
@@ -301,25 +300,30 @@ load_identity(char *filename)
301static void 300static void
302do_convert_to_ssh2(struct passwd *pw, struct sshkey *k) 301do_convert_to_ssh2(struct passwd *pw, struct sshkey *k)
303{ 302{
304 size_t len; 303 struct sshbuf *b;
305 u_char *blob; 304 char comment[61], *b64;
306 char comment[61];
307 int r; 305 int r;
308 306
309 if ((r = sshkey_to_blob(k, &blob, &len)) != 0) 307 if ((b = sshbuf_new()) == NULL)
308 fatal("%s: sshbuf_new failed", __func__);
309 if ((r = sshkey_putb(k, b)) != 0)
310 fatal("key_to_blob failed: %s", ssh_err(r)); 310 fatal("key_to_blob failed: %s", ssh_err(r));
311 if ((b64 = sshbuf_dtob64_string(b, 1)) == NULL)
312 fatal("%s: sshbuf_dtob64_string failed", __func__);
313
311 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */ 314 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
312 snprintf(comment, sizeof(comment), 315 snprintf(comment, sizeof(comment),
313 "%u-bit %s, converted by %s@%s from OpenSSH", 316 "%u-bit %s, converted by %s@%s from OpenSSH",
314 sshkey_size(k), sshkey_type(k), 317 sshkey_size(k), sshkey_type(k),
315 pw->pw_name, hostname); 318 pw->pw_name, hostname);
316 319
320 sshkey_free(k);
321 sshbuf_free(b);
322
317 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN); 323 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
318 fprintf(stdout, "Comment: \"%s\"\n", comment); 324 fprintf(stdout, "Comment: \"%s\"\n%s", comment, b64);
319 dump_base64(stdout, blob, len);
320 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END); 325 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
321 sshkey_free(k); 326 free(b64);
322 free(blob);
323 exit(0); 327 exit(0);
324} 328}
325 329
@@ -413,9 +417,8 @@ buffer_get_bignum_bits(struct sshbuf *b, BIGNUM *value)
413} 417}
414 418
415static struct sshkey * 419static struct sshkey *
416do_convert_private_ssh2_from_blob(u_char *blob, u_int blen) 420do_convert_private_ssh2(struct sshbuf *b)
417{ 421{
418 struct sshbuf *b;
419 struct sshkey *key = NULL; 422 struct sshkey *key = NULL;
420 char *type, *cipher; 423 char *type, *cipher;
421 u_char e1, e2, e3, *sig = NULL, data[] = "abcde12345"; 424 u_char e1, e2, e3, *sig = NULL, data[] = "abcde12345";
@@ -427,15 +430,13 @@ do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
427 BIGNUM *dsa_pub_key = NULL, *dsa_priv_key = NULL; 430 BIGNUM *dsa_pub_key = NULL, *dsa_priv_key = NULL;
428 BIGNUM *rsa_n = NULL, *rsa_e = NULL, *rsa_d = NULL; 431 BIGNUM *rsa_n = NULL, *rsa_e = NULL, *rsa_d = NULL;
429 BIGNUM *rsa_p = NULL, *rsa_q = NULL, *rsa_iqmp = NULL; 432 BIGNUM *rsa_p = NULL, *rsa_q = NULL, *rsa_iqmp = NULL;
430 if ((b = sshbuf_from(blob, blen)) == NULL) 433
431 fatal("%s: sshbuf_from failed", __func__);
432 if ((r = sshbuf_get_u32(b, &magic)) != 0) 434 if ((r = sshbuf_get_u32(b, &magic)) != 0)
433 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 435 fatal("%s: buffer error: %s", __func__, ssh_err(r));
434 436
435 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) { 437 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
436 error("bad magic 0x%x != 0x%x", magic, 438 error("bad magic 0x%x != 0x%x", magic,
437 SSH_COM_PRIVATE_KEY_MAGIC); 439 SSH_COM_PRIVATE_KEY_MAGIC);
438 sshbuf_free(b);
439 return NULL; 440 return NULL;
440 } 441 }
441 if ((r = sshbuf_get_u32(b, &i1)) != 0 || 442 if ((r = sshbuf_get_u32(b, &i1)) != 0 ||
@@ -449,7 +450,6 @@ do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
449 if (strcmp(cipher, "none") != 0) { 450 if (strcmp(cipher, "none") != 0) {
450 error("unsupported cipher %s", cipher); 451 error("unsupported cipher %s", cipher);
451 free(cipher); 452 free(cipher);
452 sshbuf_free(b);
453 free(type); 453 free(type);
454 return NULL; 454 return NULL;
455 } 455 }
@@ -460,7 +460,6 @@ do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
460 } else if (strstr(type, "rsa")) { 460 } else if (strstr(type, "rsa")) {
461 ktype = KEY_RSA; 461 ktype = KEY_RSA;
462 } else { 462 } else {
463 sshbuf_free(b);
464 free(type); 463 free(type);
465 return NULL; 464 return NULL;
466 } 465 }
@@ -507,7 +506,6 @@ do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
507 fatal("%s: BN_new", __func__); 506 fatal("%s: BN_new", __func__);
508 if (!BN_set_word(rsa_e, e)) { 507 if (!BN_set_word(rsa_e, e)) {
509 BN_clear_free(rsa_e); 508 BN_clear_free(rsa_e);
510 sshbuf_free(b);
511 sshkey_free(key); 509 sshkey_free(key);
512 return NULL; 510 return NULL;
513 } 511 }
@@ -535,9 +533,7 @@ do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
535 } 533 }
536 rlen = sshbuf_len(b); 534 rlen = sshbuf_len(b);
537 if (rlen != 0) 535 if (rlen != 0)
538 error("do_convert_private_ssh2_from_blob: " 536 error("%s: remaining bytes in key blob %d", __func__, rlen);
539 "remaining bytes in key blob %d", rlen);
540 sshbuf_free(b);
541 537
542 /* try the key */ 538 /* try the key */
543 if (sshkey_sign(key, &sig, &slen, data, sizeof(data), NULL, 0) != 0 || 539 if (sshkey_sign(key, &sig, &slen, data, sizeof(data), NULL, 0) != 0 ||
@@ -582,10 +578,12 @@ do_convert_from_ssh2(struct passwd *pw, struct sshkey **k, int *private)
582 int r, blen, escaped = 0; 578 int r, blen, escaped = 0;
583 u_int len; 579 u_int len;
584 char line[1024]; 580 char line[1024];
585 u_char blob[8096]; 581 struct sshbuf *buf;
586 char encoded[8096]; 582 char encoded[8096];
587 FILE *fp; 583 FILE *fp;
588 584
585 if ((buf = sshbuf_new()) == NULL)
586 fatal("sshbuf_new failed");
589 if ((fp = fopen(identity_file, "r")) == NULL) 587 if ((fp = fopen(identity_file, "r")) == NULL)
590 fatal("%s: %s: %s", __progname, identity_file, strerror(errno)); 588 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
591 encoded[0] = '\0'; 589 encoded[0] = '\0';
@@ -615,12 +613,11 @@ do_convert_from_ssh2(struct passwd *pw, struct sshkey **k, int *private)
615 (encoded[len-2] == '=') && 613 (encoded[len-2] == '=') &&
616 (encoded[len-3] == '=')) 614 (encoded[len-3] == '='))
617 encoded[len-3] = '\0'; 615 encoded[len-3] = '\0';
618 blen = uudecode(encoded, blob, sizeof(blob)); 616 if ((r = sshbuf_b64tod(buf, encoded)) != 0)
619 if (blen < 0) 617 fatal("%s: base64 decoding failed: %s", __func__, ssh_err(r));
620 fatal("uudecode failed.");
621 if (*private) 618 if (*private)
622 *k = do_convert_private_ssh2_from_blob(blob, blen); 619 *k = do_convert_private_ssh2(buf);
623 else if ((r = sshkey_from_blob(blob, blen, k)) != 0) 620 else if ((r = sshkey_fromb(buf, k)) != 0)
624 fatal("decode blob failed: %s", ssh_err(r)); 621 fatal("decode blob failed: %s", ssh_err(r));
625 fclose(fp); 622 fclose(fp);
626} 623}
@@ -1739,7 +1736,7 @@ do_ca_sign(struct passwd *pw, const char *ca_key_path, int prefer_agent,
1739 } 1736 }
1740 if (n > SSHKEY_CERT_MAX_PRINCIPALS) 1737 if (n > SSHKEY_CERT_MAX_PRINCIPALS)
1741 fatal("Too many certificate principals specified"); 1738 fatal("Too many certificate principals specified");
1742 1739
1743 tmp = tilde_expand_filename(argv[i], pw->pw_uid); 1740 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1744 if ((r = sshkey_load_public(tmp, &public, &comment)) != 0) 1741 if ((r = sshkey_load_public(tmp, &public, &comment)) != 0)
1745 fatal("%s: unable to open \"%s\": %s", 1742 fatal("%s: unable to open \"%s\": %s",
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
diff --git a/sshbuf.h b/sshbuf.h
index 79700b54d..ea40e1677 100644
--- a/sshbuf.h
+++ b/sshbuf.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: sshbuf.h,v 1.15 2019/07/15 13:11:38 djm Exp $ */ 1/* $OpenBSD: sshbuf.h,v 1.16 2019/07/16 13:18:39 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2011 Damien Miller 3 * Copyright (c) 2011 Damien Miller
4 * 4 *
@@ -251,7 +251,8 @@ void sshbuf_dump_data(const void *s, size_t len, FILE *f);
251char *sshbuf_dtob16(struct sshbuf *buf); 251char *sshbuf_dtob16(struct sshbuf *buf);
252 252
253/* Encode the contents of the buffer as base64 */ 253/* Encode the contents of the buffer as base64 */
254char *sshbuf_dtob64(struct sshbuf *buf); 254char *sshbuf_dtob64_string(const struct sshbuf *buf, int wrap);
255int sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap);
255 256
256/* Decode base64 data and append it to the buffer */ 257/* Decode base64 data and append it to the buffer */
257int sshbuf_b64tod(struct sshbuf *buf, const char *b64); 258int sshbuf_b64tod(struct sshbuf *buf, const char *b64);
diff --git a/sshkey.c b/sshkey.c
index a0cea9257..9956a08b9 100644
--- a/sshkey.c
+++ b/sshkey.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: sshkey.c,v 1.80 2019/07/15 13:16:29 djm Exp $ */ 1/* $OpenBSD: sshkey.c,v 1.81 2019/07/16 13:18:39 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. 3 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
4 * Copyright (c) 2008 Alexander von Gernler. All rights reserved. 4 * Copyright (c) 2008 Alexander von Gernler. All rights reserved.
@@ -1402,7 +1402,7 @@ sshkey_to_base64(const struct sshkey *key, char **b64p)
1402 return SSH_ERR_ALLOC_FAIL; 1402 return SSH_ERR_ALLOC_FAIL;
1403 if ((r = sshkey_putb(key, b)) != 0) 1403 if ((r = sshkey_putb(key, b)) != 0)
1404 goto out; 1404 goto out;
1405 if ((uu = sshbuf_dtob64(b)) == NULL) { 1405 if ((uu = sshbuf_dtob64_string(b, 0)) == NULL) {
1406 r = SSH_ERR_ALLOC_FAIL; 1406 r = SSH_ERR_ALLOC_FAIL;
1407 goto out; 1407 goto out;
1408 } 1408 }
@@ -3709,25 +3709,12 @@ sshkey_private_to_blob2(struct sshkey *prv, struct sshbuf *blob,
3709 sshbuf_ptr(encrypted), sshbuf_len(encrypted), 0, authlen)) != 0) 3709 sshbuf_ptr(encrypted), sshbuf_len(encrypted), 0, authlen)) != 0)
3710 goto out; 3710 goto out;
3711 3711
3712 /* uuencode */
3713 if ((b64 = sshbuf_dtob64(encoded)) == NULL) {
3714 r = SSH_ERR_ALLOC_FAIL;
3715 goto out;
3716 }
3717
3718 sshbuf_reset(blob); 3712 sshbuf_reset(blob);
3719 if ((r = sshbuf_put(blob, MARK_BEGIN, MARK_BEGIN_LEN)) != 0) 3713
3720 goto out; 3714 /* assemble uuencoded key */
3721 for (i = 0; i < strlen(b64); i++) { 3715 if ((r = sshbuf_put(blob, MARK_BEGIN, MARK_BEGIN_LEN)) != 0 ||
3722 if ((r = sshbuf_put_u8(blob, b64[i])) != 0) 3716 (r = sshbuf_dtob64(encoded, blob, 1)) != 0 ||
3723 goto out; 3717 (r = sshbuf_put(blob, MARK_END, MARK_END_LEN)) != 0)
3724 /* insert line breaks */
3725 if (i % 70 == 69 && (r = sshbuf_put_u8(blob, '\n')) != 0)
3726 goto out;
3727 }
3728 if (i % 70 != 69 && (r = sshbuf_put_u8(blob, '\n')) != 0)
3729 goto out;
3730 if ((r = sshbuf_put(blob, MARK_END, MARK_END_LEN)) != 0)
3731 goto out; 3718 goto out;
3732 3719
3733 /* success */ 3720 /* success */
diff --git a/uuencode.c b/uuencode.c
deleted file mode 100644
index 7fc867a11..000000000
--- a/uuencode.c
+++ /dev/null
@@ -1,95 +0,0 @@
1/* $OpenBSD: uuencode.c,v 1.28 2015/04/24 01:36:24 deraadt Exp $ */
2/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "includes.h"
27
28#include <sys/types.h>
29#include <netinet/in.h>
30#include <resolv.h>
31#include <stdio.h>
32#include <stdlib.h>
33
34#include "xmalloc.h"
35#include "uuencode.h"
36
37/*
38 * Encode binary 'src' of length 'srclength', writing base64-encoded text
39 * to 'target' of size 'targsize'. Will always nul-terminate 'target'.
40 * Returns the number of bytes stored in 'target' or -1 on error (inc.
41 * 'targsize' too small).
42 */
43int
44uuencode(const u_char *src, u_int srclength,
45 char *target, size_t targsize)
46{
47 return __b64_ntop(src, srclength, target, targsize);
48}
49
50/*
51 * Decode base64-encoded 'src' into buffer 'target' of 'targsize' bytes.
52 * Will skip leading and trailing whitespace. Returns the number of bytes
53 * stored in 'target' or -1 on error (inc. targsize too small).
54 */
55int
56uudecode(const char *src, u_char *target, size_t targsize)
57{
58 int len;
59 char *encoded, *p;
60
61 /* copy the 'readonly' source */
62 encoded = xstrdup(src);
63 /* skip whitespace and data */
64 for (p = encoded; *p == ' ' || *p == '\t'; p++)
65 ;
66 for (; *p != '\0' && *p != ' ' && *p != '\t'; p++)
67 ;
68 /* and remove trailing whitespace because __b64_pton needs this */
69 *p = '\0';
70 len = __b64_pton(encoded, target, targsize);
71 free(encoded);
72 return len;
73}
74
75void
76dump_base64(FILE *fp, const u_char *data, u_int len)
77{
78 char *buf;
79 int i, n;
80
81 if (len > 65536) {
82 fprintf(fp, "dump_base64: len > 65536\n");
83 return;
84 }
85 buf = xreallocarray(NULL, 2, len);
86 n = uuencode(data, len, buf, 2*len);
87 for (i = 0; i < n; i++) {
88 fprintf(fp, "%c", buf[i]);
89 if (i % 70 == 69)
90 fprintf(fp, "\n");
91 }
92 if (i % 70 != 69)
93 fprintf(fp, "\n");
94 free(buf);
95}
diff --git a/uuencode.h b/uuencode.h
deleted file mode 100644
index 4d9888126..000000000
--- a/uuencode.h
+++ /dev/null
@@ -1,29 +0,0 @@
1/* $OpenBSD: uuencode.h,v 1.14 2010/08/31 11:54:45 djm Exp $ */
2
3/*
4 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27int uuencode(const u_char *, u_int, char *, size_t);
28int uudecode(const char *, u_char *, size_t);
29void dump_base64(FILE *, const u_char *, u_int);