1999-05-18 17:46:59 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
1999-05-18 17:46:59 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
1999-05-18 17:46:59 +00:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
1999-05-18 17:46:59 +00:00
|
|
|
#include <isc/base64.h>
|
|
|
|
#include <isc/buffer.h>
|
|
|
|
#include <isc/lex.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
2000-04-28 01:12:23 +00:00
|
|
|
#include <isc/util.h>
|
1999-05-18 17:46:59 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define RETERR(x) \
|
|
|
|
do { \
|
|
|
|
isc_result_t _r = (x); \
|
|
|
|
if (_r != ISC_R_SUCCESS) \
|
2020-02-13 21:48:23 +01:00
|
|
|
return ((_r)); \
|
1999-05-18 17:46:59 +00:00
|
|
|
} while (0)
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*@{*/
|
|
|
|
/*!
|
2000-05-08 14:38:29 +00:00
|
|
|
* These static functions are also present in lib/dns/rdata.c. I'm not
|
1999-05-18 17:46:59 +00:00
|
|
|
* sure where they should go. -- bwelling
|
|
|
|
*/
|
2020-02-14 08:14:03 +01:00
|
|
|
static isc_result_t
|
|
|
|
str_totext(const char *source, isc_buffer_t *target);
|
2000-06-01 17:20:56 +00:00
|
|
|
|
2020-02-14 08:14:03 +01:00
|
|
|
static isc_result_t
|
|
|
|
mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
|
1999-05-18 17:46:59 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw"
|
|
|
|
"xyz0123456789+/=";
|
2005-04-27 04:57:32 +00:00
|
|
|
/*@}*/
|
1999-05-18 17:46:59 +00:00
|
|
|
|
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_base64_totext(isc_region_t *source, int wordlength, const char *wordbreak,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_buffer_t *target) {
|
|
|
|
char buf[5];
|
1999-06-08 10:35:23 +00:00
|
|
|
unsigned int loops = 0;
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (wordlength < 4) {
|
1999-06-08 10:35:23 +00:00
|
|
|
wordlength = 4;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-05-18 17:46:59 +00:00
|
|
|
|
2001-11-27 01:56:32 +00:00
|
|
|
memset(buf, 0, sizeof(buf));
|
1999-05-18 17:46:59 +00:00
|
|
|
while (source->length > 2) {
|
2020-02-12 13:59:18 +01:00
|
|
|
buf[0] = base64[(source->base[0] >> 2) & 0x3f];
|
|
|
|
buf[1] = base64[((source->base[0] << 4) & 0x30) |
|
|
|
|
((source->base[1] >> 4) & 0x0f)];
|
|
|
|
buf[2] = base64[((source->base[1] << 2) & 0x3c) |
|
|
|
|
((source->base[2] >> 6) & 0x03)];
|
|
|
|
buf[3] = base64[source->base[2] & 0x3f];
|
1999-05-18 17:46:59 +00:00
|
|
|
RETERR(str_totext(buf, target));
|
|
|
|
isc_region_consume(source, 3);
|
1999-06-08 10:35:23 +00:00
|
|
|
|
|
|
|
loops++;
|
2020-02-13 14:44:37 -08:00
|
|
|
if (source->length != 0 && (int)((loops + 1) * 4) >= wordlength)
|
|
|
|
{
|
1999-05-18 17:46:59 +00:00
|
|
|
loops = 0;
|
1999-06-08 10:35:23 +00:00
|
|
|
RETERR(str_totext(wordbreak, target));
|
1999-05-18 17:46:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (source->length == 2) {
|
2020-02-12 13:59:18 +01:00
|
|
|
buf[0] = base64[(source->base[0] >> 2) & 0x3f];
|
|
|
|
buf[1] = base64[((source->base[0] << 4) & 0x30) |
|
|
|
|
((source->base[1] >> 4) & 0x0f)];
|
|
|
|
buf[2] = base64[((source->base[1] << 2) & 0x3c)];
|
1999-05-18 17:46:59 +00:00
|
|
|
buf[3] = '=';
|
|
|
|
RETERR(str_totext(buf, target));
|
2009-10-21 01:22:29 +00:00
|
|
|
isc_region_consume(source, 2);
|
1999-05-18 17:46:59 +00:00
|
|
|
} else if (source->length == 1) {
|
2020-02-12 13:59:18 +01:00
|
|
|
buf[0] = base64[(source->base[0] >> 2) & 0x3f];
|
|
|
|
buf[1] = base64[((source->base[0] << 4) & 0x30)];
|
1999-05-18 17:46:59 +00:00
|
|
|
buf[2] = buf[3] = '=';
|
|
|
|
RETERR(str_totext(buf, target));
|
2009-10-21 01:22:29 +00:00
|
|
|
isc_region_consume(source, 1);
|
1999-05-18 17:46:59 +00:00
|
|
|
}
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
2000-09-08 00:34:21 +00:00
|
|
|
* State of a base64 decoding process in progress.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
2020-02-13 14:44:37 -08:00
|
|
|
int length; /*%< Desired length of binary data or -1 */
|
|
|
|
isc_buffer_t *target; /*%< Buffer for resulting binary data */
|
|
|
|
int digits; /*%< Number of buffered base64 digits */
|
|
|
|
bool seen_end; /*%< True if "=" end marker seen */
|
|
|
|
int val[4];
|
2000-09-08 00:34:21 +00:00
|
|
|
} base64_decode_ctx_t;
|
|
|
|
|
|
|
|
static inline void
|
2020-02-13 14:44:37 -08:00
|
|
|
base64_decode_init(base64_decode_ctx_t *ctx, int length, isc_buffer_t *target) {
|
2000-09-08 00:34:21 +00:00
|
|
|
ctx->digits = 0;
|
2018-04-17 08:29:14 -07:00
|
|
|
ctx->seen_end = false;
|
2000-09-08 00:34:21 +00:00
|
|
|
ctx->length = length;
|
|
|
|
ctx->target = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
base64_decode_char(base64_decode_ctx_t *ctx, int c) {
|
2015-11-20 18:38:24 +11:00
|
|
|
const char *s;
|
2000-09-08 00:34:21 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ctx->seen_end) {
|
2000-09-08 00:34:21 +00:00
|
|
|
return (ISC_R_BADBASE64);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if ((s = strchr(base64, c)) == NULL) {
|
2000-09-08 00:34:21 +00:00
|
|
|
return (ISC_R_BADBASE64);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2013-12-04 12:47:23 +11:00
|
|
|
ctx->val[ctx->digits++] = (int)(s - base64);
|
2000-09-08 00:34:21 +00:00
|
|
|
if (ctx->digits == 4) {
|
2020-02-13 14:44:37 -08:00
|
|
|
int n;
|
2000-09-08 00:34:21 +00:00
|
|
|
unsigned char buf[3];
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ctx->val[0] == 64 || ctx->val[1] == 64) {
|
2000-09-08 00:34:21 +00:00
|
|
|
return (ISC_R_BADBASE64);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (ctx->val[2] == 64 && ctx->val[3] != 64) {
|
2000-09-08 00:34:21 +00:00
|
|
|
return (ISC_R_BADBASE64);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2003-04-17 06:04:13 +00:00
|
|
|
/*
|
|
|
|
* Check that bits that should be zero are.
|
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ctx->val[2] == 64 && (ctx->val[1] & 0xf) != 0) {
|
2003-04-17 06:04:13 +00:00
|
|
|
return (ISC_R_BADBASE64);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2003-04-17 06:04:13 +00:00
|
|
|
/*
|
|
|
|
* We don't need to test for ctx->val[2] != 64 as
|
|
|
|
* the bottom two bits of 64 are zero.
|
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ctx->val[3] == 64 && (ctx->val[2] & 0x3) != 0) {
|
2003-04-17 06:04:13 +00:00
|
|
|
return (ISC_R_BADBASE64);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
n = (ctx->val[2] == 64) ? 1 : (ctx->val[3] == 64) ? 2 : 3;
|
2000-09-08 00:34:21 +00:00
|
|
|
if (n != 3) {
|
2018-04-17 08:29:14 -07:00
|
|
|
ctx->seen_end = true;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ctx->val[2] == 64) {
|
2000-09-08 00:34:21 +00:00
|
|
|
ctx->val[2] = 0;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (ctx->val[3] == 64) {
|
2000-09-08 00:34:21 +00:00
|
|
|
ctx->val[3] = 0;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-09-08 00:34:21 +00:00
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
buf[0] = (ctx->val[0] << 2) | (ctx->val[1] >> 4);
|
|
|
|
buf[1] = (ctx->val[1] << 4) | (ctx->val[2] >> 2);
|
|
|
|
buf[2] = (ctx->val[2] << 6) | (ctx->val[3]);
|
2000-09-08 00:34:21 +00:00
|
|
|
RETERR(mem_tobuffer(ctx->target, buf, n));
|
|
|
|
if (ctx->length >= 0) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (n > ctx->length) {
|
2000-09-08 00:34:21 +00:00
|
|
|
return (ISC_R_BADBASE64);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2000-09-08 00:34:21 +00:00
|
|
|
ctx->length -= n;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-09-08 00:34:21 +00:00
|
|
|
}
|
|
|
|
ctx->digits = 0;
|
|
|
|
}
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
base64_decode_finish(base64_decode_ctx_t *ctx) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ctx->length > 0) {
|
2000-09-08 00:34:21 +00:00
|
|
|
return (ISC_R_UNEXPECTEDEND);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (ctx->digits != 0) {
|
2000-09-08 00:34:21 +00:00
|
|
|
return (ISC_R_BADBASE64);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-09-08 00:34:21 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1999-05-18 17:46:59 +00:00
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_base64_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
|
|
|
|
unsigned int before, after;
|
2000-09-08 00:34:21 +00:00
|
|
|
base64_decode_ctx_t ctx;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_textregion_t *tr;
|
|
|
|
isc_token_t token;
|
|
|
|
bool eol;
|
1999-05-18 17:46:59 +00:00
|
|
|
|
2019-01-04 15:20:04 +11:00
|
|
|
REQUIRE(length >= -2);
|
|
|
|
|
2000-09-08 00:34:21 +00:00
|
|
|
base64_decode_init(&ctx, length, target);
|
|
|
|
|
2019-01-04 15:20:04 +11:00
|
|
|
before = isc_buffer_usedlength(target);
|
2000-09-08 00:34:21 +00:00
|
|
|
while (!ctx.seen_end && (ctx.length != 0)) {
|
|
|
|
unsigned int i;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2019-01-04 15:20:04 +11:00
|
|
|
if (length > 0) {
|
2018-04-17 08:29:14 -07:00
|
|
|
eol = false;
|
2019-01-04 15:20:04 +11:00
|
|
|
} else {
|
2018-04-17 08:29:14 -07:00
|
|
|
eol = true;
|
2019-01-04 15:20:04 +11:00
|
|
|
}
|
2000-11-08 01:56:15 +00:00
|
|
|
RETERR(isc_lex_getmastertoken(lexer, &token,
|
|
|
|
isc_tokentype_string, eol));
|
2019-01-04 15:20:04 +11:00
|
|
|
if (token.type != isc_tokentype_string) {
|
1999-05-18 17:46:59 +00:00
|
|
|
break;
|
2019-01-04 15:20:04 +11:00
|
|
|
}
|
1999-05-18 17:46:59 +00:00
|
|
|
tr = &token.value.as_textregion;
|
2019-01-04 15:20:04 +11:00
|
|
|
for (i = 0; i < tr->length; i++) {
|
2000-09-08 00:34:21 +00:00
|
|
|
RETERR(base64_decode_char(&ctx, tr->base[i]));
|
2019-01-04 15:20:04 +11:00
|
|
|
}
|
1999-05-18 17:46:59 +00:00
|
|
|
}
|
2019-01-04 15:20:04 +11:00
|
|
|
after = isc_buffer_usedlength(target);
|
|
|
|
if (ctx.length < 0 && !ctx.seen_end) {
|
1999-05-18 17:46:59 +00:00
|
|
|
isc_lex_ungettoken(lexer, &token);
|
2019-01-04 15:20:04 +11:00
|
|
|
}
|
2000-09-08 00:34:21 +00:00
|
|
|
RETERR(base64_decode_finish(&ctx));
|
2019-01-04 15:20:04 +11:00
|
|
|
if (length == -2 && before == after) {
|
|
|
|
return (ISC_R_UNEXPECTEDEND);
|
|
|
|
}
|
1999-05-18 17:46:59 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2000-07-11 21:51:13 +00:00
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_base64_decodestring(const char *cstr, isc_buffer_t *target) {
|
2000-09-08 00:34:21 +00:00
|
|
|
base64_decode_ctx_t ctx;
|
2000-07-11 21:51:13 +00:00
|
|
|
|
2000-09-08 00:34:21 +00:00
|
|
|
base64_decode_init(&ctx, -1, target);
|
|
|
|
for (;;) {
|
|
|
|
int c = *cstr++;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (c == '\0') {
|
2000-09-08 00:34:21 +00:00
|
|
|
break;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
|
2000-09-08 00:34:21 +00:00
|
|
|
continue;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-09-08 00:34:21 +00:00
|
|
|
RETERR(base64_decode_char(&ctx, c));
|
2000-08-01 01:33:37 +00:00
|
|
|
}
|
2009-10-21 23:48:05 +00:00
|
|
|
RETERR(base64_decode_finish(&ctx));
|
2000-09-08 00:34:21 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
2000-07-11 21:51:13 +00:00
|
|
|
}
|
|
|
|
|
1999-05-18 17:46:59 +00:00
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
str_totext(const char *source, isc_buffer_t *target) {
|
1999-05-18 17:46:59 +00:00
|
|
|
unsigned int l;
|
|
|
|
isc_region_t region;
|
|
|
|
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_availableregion(target, ®ion);
|
1999-05-18 17:46:59 +00:00
|
|
|
l = strlen(source);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (l > region.length) {
|
1999-05-18 17:46:59 +00:00
|
|
|
return (ISC_R_NOSPACE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-05-18 17:46:59 +00:00
|
|
|
|
2014-01-08 16:27:10 -08:00
|
|
|
memmove(region.base, source, l);
|
1999-05-18 17:46:59 +00:00
|
|
|
isc_buffer_add(target, l);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
|
1999-05-18 17:46:59 +00:00
|
|
|
isc_region_t tr;
|
|
|
|
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_availableregion(target, &tr);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (length > tr.length) {
|
1999-05-18 17:46:59 +00:00
|
|
|
return (ISC_R_NOSPACE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-01-08 16:27:10 -08:00
|
|
|
memmove(tr.base, base, length);
|
1999-05-18 17:46:59 +00:00
|
|
|
isc_buffer_add(target, length);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|