=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/ssh/sshbuf-misc.c,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- src/usr.bin/ssh/sshbuf-misc.c 2020/06/05 03:24:36 1.15 +++ src/usr.bin/ssh/sshbuf-misc.c 2020/06/22 05:54:10 1.16 @@ -1,4 +1,4 @@ -/* $OpenBSD: sshbuf-misc.c,v 1.15 2020/06/05 03:24:36 djm Exp $ */ +/* $OpenBSD: sshbuf-misc.c,v 1.16 2020/06/22 05:54:10 djm Exp $ */ /* * Copyright (c) 2011 Damien Miller * @@ -161,6 +161,49 @@ } freezero(p, plen); return 0; +} + +int +sshbuf_dtourlb64(const struct sshbuf *d, struct sshbuf *b64, int wrap) +{ + int r = SSH_ERR_INTERNAL_ERROR; + u_char *p; + struct sshbuf *b = NULL; + size_t i, l; + + if ((b = sshbuf_new()) == NULL) + return SSH_ERR_ALLOC_FAIL; + /* Encode using regular base64; we'll transform it once done */ + if ((r = sshbuf_dtob64(d, b, wrap)) != 0) + goto out; + /* remove padding from end of encoded string*/ + for (;;) { + l = sshbuf_len(b); + if (l <= 1 || sshbuf_ptr(b) == NULL) { + r = SSH_ERR_INTERNAL_ERROR; + goto out; + } + if (sshbuf_ptr(b)[l - 1] != '=') + break; + if ((r = sshbuf_consume_end(b, 1)) != 0) + goto out; + } + /* Replace characters with rfc4648 equivalents */ + l = sshbuf_len(b); + if ((p = sshbuf_mutable_ptr(b)) == NULL) { + r = SSH_ERR_INTERNAL_ERROR; + goto out; + } + for (i = 0; i < l; i++) { + if (p[i] == '+') + p[i] = '-'; + else if (p[i] == '/') + p[i] = '_'; + } + r = sshbuf_putb(b64, b); + out: + sshbuf_free(b); + return r; } char *