diff options
-rw-r--r-- | sshbuf-misc.c | 45 | ||||
-rw-r--r-- | sshbuf.h | 4 |
2 files changed, 47 insertions, 2 deletions
diff --git a/sshbuf-misc.c b/sshbuf-misc.c index 86e5fa34f..afaab8d61 100644 --- a/sshbuf-misc.c +++ b/sshbuf-misc.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: sshbuf-misc.c,v 1.15 2020/06/05 03:24:36 djm Exp $ */ | 1 | /* $OpenBSD: sshbuf-misc.c,v 1.16 2020/06/22 05:54:10 djm Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2011 Damien Miller | 3 | * Copyright (c) 2011 Damien Miller |
4 | * | 4 | * |
@@ -167,6 +167,49 @@ sshbuf_b64tod(struct sshbuf *buf, const char *b64) | |||
167 | return 0; | 167 | return 0; |
168 | } | 168 | } |
169 | 169 | ||
170 | int | ||
171 | sshbuf_dtourlb64(const struct sshbuf *d, struct sshbuf *b64, int wrap) | ||
172 | { | ||
173 | int r = SSH_ERR_INTERNAL_ERROR; | ||
174 | u_char *p; | ||
175 | struct sshbuf *b = NULL; | ||
176 | size_t i, l; | ||
177 | |||
178 | if ((b = sshbuf_new()) == NULL) | ||
179 | return SSH_ERR_ALLOC_FAIL; | ||
180 | /* Encode using regular base64; we'll transform it once done */ | ||
181 | if ((r = sshbuf_dtob64(d, b, wrap)) != 0) | ||
182 | goto out; | ||
183 | /* remove padding from end of encoded string*/ | ||
184 | for (;;) { | ||
185 | l = sshbuf_len(b); | ||
186 | if (l <= 1 || sshbuf_ptr(b) == NULL) { | ||
187 | r = SSH_ERR_INTERNAL_ERROR; | ||
188 | goto out; | ||
189 | } | ||
190 | if (sshbuf_ptr(b)[l - 1] != '=') | ||
191 | break; | ||
192 | if ((r = sshbuf_consume_end(b, 1)) != 0) | ||
193 | goto out; | ||
194 | } | ||
195 | /* Replace characters with rfc4648 equivalents */ | ||
196 | l = sshbuf_len(b); | ||
197 | if ((p = sshbuf_mutable_ptr(b)) == NULL) { | ||
198 | r = SSH_ERR_INTERNAL_ERROR; | ||
199 | goto out; | ||
200 | } | ||
201 | for (i = 0; i < l; i++) { | ||
202 | if (p[i] == '+') | ||
203 | p[i] = '-'; | ||
204 | else if (p[i] == '/') | ||
205 | p[i] = '_'; | ||
206 | } | ||
207 | r = sshbuf_putb(b64, b); | ||
208 | out: | ||
209 | sshbuf_free(b); | ||
210 | return r; | ||
211 | } | ||
212 | |||
170 | char * | 213 | char * |
171 | sshbuf_dup_string(struct sshbuf *buf) | 214 | sshbuf_dup_string(struct sshbuf *buf) |
172 | { | 215 | { |
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: sshbuf.h,v 1.22 2020/06/05 03:24:36 djm Exp $ */ | 1 | /* $OpenBSD: sshbuf.h,v 1.23 2020/06/22 05:54:10 djm Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2011 Damien Miller | 3 | * Copyright (c) 2011 Damien Miller |
4 | * | 4 | * |
@@ -253,6 +253,8 @@ char *sshbuf_dtob16(struct sshbuf *buf); | |||
253 | /* Encode the contents of the buffer as base64 */ | 253 | /* Encode the contents of the buffer as base64 */ |
254 | char *sshbuf_dtob64_string(const struct sshbuf *buf, int wrap); | 254 | char *sshbuf_dtob64_string(const struct sshbuf *buf, int wrap); |
255 | int sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap); | 255 | int sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap); |
256 | /* RFC4648 "base64url" encoding variant */ | ||
257 | int sshbuf_dtourlb64(const struct sshbuf *d, struct sshbuf *b64, int wrap); | ||
256 | 258 | ||
257 | /* Decode base64 data and append it to the buffer */ | 259 | /* Decode base64 data and append it to the buffer */ |
258 | int sshbuf_b64tod(struct sshbuf *buf, const char *b64); | 260 | int sshbuf_b64tod(struct sshbuf *buf, const char *b64); |