Blame lib/isc/base64.c

Packit 5ce601
/*
Packit 5ce601
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
Packit 5ce601
 *
Packit 5ce601
 * This Source Code Form is subject to the terms of the Mozilla Public
Packit 5ce601
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit Service 704ed8
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
Packit 5ce601
 *
Packit 5ce601
 * See the COPYRIGHT file distributed with this work for additional
Packit 5ce601
 * information regarding copyright ownership.
Packit 5ce601
 */
Packit 5ce601
Packit 5ce601
Packit 5ce601
/*! \file */
Packit 5ce601
Packit 5ce601
#include <config.h>
Packit 5ce601
Packit 5ce601
#include <stdbool.h>
Packit 5ce601
Packit 5ce601
#include <isc/base64.h>
Packit 5ce601
#include <isc/buffer.h>
Packit 5ce601
#include <isc/lex.h>
Packit 5ce601
#include <isc/string.h>
Packit 5ce601
#include <isc/util.h>
Packit 5ce601
Packit 5ce601
#define RETERR(x) do { \
Packit 5ce601
	isc_result_t _r = (x); \
Packit 5ce601
	if (_r != ISC_R_SUCCESS) \
Packit 5ce601
		return (_r); \
Packit 5ce601
	} while (0)
Packit 5ce601
Packit 5ce601
Packit 5ce601
/*@{*/
Packit 5ce601
/*!
Packit 5ce601
 * These static functions are also present in lib/dns/rdata.c.  I'm not
Packit 5ce601
 * sure where they should go. -- bwelling
Packit 5ce601
 */
Packit 5ce601
static isc_result_t
Packit 5ce601
str_totext(const char *source, isc_buffer_t *target);
Packit 5ce601
Packit 5ce601
static isc_result_t
Packit 5ce601
mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
Packit 5ce601
Packit 5ce601
static const char base64[] =
Packit 5ce601
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
Packit 5ce601
/*@}*/
Packit 5ce601
Packit 5ce601
isc_result_t
Packit 5ce601
isc_base64_totext(isc_region_t *source, int wordlength,
Packit 5ce601
		  const char *wordbreak, isc_buffer_t *target)
Packit 5ce601
{
Packit 5ce601
	char buf[5];
Packit 5ce601
	unsigned int loops = 0;
Packit 5ce601
Packit 5ce601
	if (wordlength < 4)
Packit 5ce601
		wordlength = 4;
Packit 5ce601
Packit 5ce601
	memset(buf, 0, sizeof(buf));
Packit 5ce601
	while (source->length > 2) {
Packit 5ce601
		buf[0] = base64[(source->base[0]>>2)&0x3f];
Packit 5ce601
		buf[1] = base64[((source->base[0]<<4)&0x30)|
Packit 5ce601
				((source->base[1]>>4)&0x0f)];
Packit 5ce601
		buf[2] = base64[((source->base[1]<<2)&0x3c)|
Packit 5ce601
				((source->base[2]>>6)&0x03)];
Packit 5ce601
		buf[3] = base64[source->base[2]&0x3f];
Packit 5ce601
		RETERR(str_totext(buf, target));
Packit 5ce601
		isc_region_consume(source, 3);
Packit 5ce601
Packit 5ce601
		loops++;
Packit 5ce601
		if (source->length != 0 &&
Packit 5ce601
		    (int)((loops + 1) * 4) >= wordlength)
Packit 5ce601
		{
Packit 5ce601
			loops = 0;
Packit 5ce601
			RETERR(str_totext(wordbreak, target));
Packit 5ce601
		}
Packit 5ce601
	}
Packit 5ce601
	if (source->length == 2) {
Packit 5ce601
		buf[0] = base64[(source->base[0]>>2)&0x3f];
Packit 5ce601
		buf[1] = base64[((source->base[0]<<4)&0x30)|
Packit 5ce601
				((source->base[1]>>4)&0x0f)];
Packit 5ce601
		buf[2] = base64[((source->base[1]<<2)&0x3c)];
Packit 5ce601
		buf[3] = '=';
Packit 5ce601
		RETERR(str_totext(buf, target));
Packit 5ce601
		isc_region_consume(source, 2);
Packit 5ce601
	} else if (source->length == 1) {
Packit 5ce601
		buf[0] = base64[(source->base[0]>>2)&0x3f];
Packit 5ce601
		buf[1] = base64[((source->base[0]<<4)&0x30)];
Packit 5ce601
		buf[2] = buf[3] = '=';
Packit 5ce601
		RETERR(str_totext(buf, target));
Packit 5ce601
		isc_region_consume(source, 1);
Packit 5ce601
	}
Packit 5ce601
	return (ISC_R_SUCCESS);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
/*%
Packit 5ce601
 * State of a base64 decoding process in progress.
Packit 5ce601
 */
Packit 5ce601
typedef struct {
Packit 5ce601
	int length;		/*%< Desired length of binary data or -1 */
Packit 5ce601
	isc_buffer_t *target;	/*%< Buffer for resulting binary data */
Packit 5ce601
	int digits;		/*%< Number of buffered base64 digits */
Packit 5ce601
	bool seen_end;	/*%< True if "=" end marker seen */
Packit 5ce601
	int val[4];
Packit 5ce601
} base64_decode_ctx_t;
Packit 5ce601
Packit 5ce601
static inline void
Packit 5ce601
base64_decode_init(base64_decode_ctx_t *ctx, int length, isc_buffer_t *target)
Packit 5ce601
{
Packit 5ce601
	ctx->digits = 0;
Packit 5ce601
	ctx->seen_end = false;
Packit 5ce601
	ctx->length = length;
Packit 5ce601
	ctx->target = target;
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static inline isc_result_t
Packit 5ce601
base64_decode_char(base64_decode_ctx_t *ctx, int c) {
Packit 5ce601
	const char *s;
Packit 5ce601
Packit 5ce601
	if (ctx->seen_end)
Packit 5ce601
		return (ISC_R_BADBASE64);
Packit 5ce601
	if ((s = strchr(base64, c)) == NULL)
Packit 5ce601
		return (ISC_R_BADBASE64);
Packit 5ce601
	ctx->val[ctx->digits++] = (int)(s - base64);
Packit 5ce601
	if (ctx->digits == 4) {
Packit 5ce601
		int n;
Packit 5ce601
		unsigned char buf[3];
Packit 5ce601
		if (ctx->val[0] == 64 || ctx->val[1] == 64)
Packit 5ce601
			return (ISC_R_BADBASE64);
Packit 5ce601
		if (ctx->val[2] == 64 && ctx->val[3] != 64)
Packit 5ce601
			return (ISC_R_BADBASE64);
Packit 5ce601
		/*
Packit 5ce601
		 * Check that bits that should be zero are.
Packit 5ce601
		 */
Packit 5ce601
		if (ctx->val[2] == 64 && (ctx->val[1] & 0xf) != 0)
Packit 5ce601
			return (ISC_R_BADBASE64);
Packit 5ce601
		/*
Packit 5ce601
		 * We don't need to test for ctx->val[2] != 64 as
Packit 5ce601
		 * the bottom two bits of 64 are zero.
Packit 5ce601
		 */
Packit 5ce601
		if (ctx->val[3] == 64 && (ctx->val[2] & 0x3) != 0)
Packit 5ce601
			return (ISC_R_BADBASE64);
Packit 5ce601
		n = (ctx->val[2] == 64) ? 1 :
Packit 5ce601
			(ctx->val[3] == 64) ? 2 : 3;
Packit 5ce601
		if (n != 3) {
Packit 5ce601
			ctx->seen_end = true;
Packit 5ce601
			if (ctx->val[2] == 64)
Packit 5ce601
				ctx->val[2] = 0;
Packit 5ce601
			if (ctx->val[3] == 64)
Packit 5ce601
				ctx->val[3] = 0;
Packit 5ce601
		}
Packit 5ce601
		buf[0] = (ctx->val[0]<<2)|(ctx->val[1]>>4);
Packit 5ce601
		buf[1] = (ctx->val[1]<<4)|(ctx->val[2]>>2);
Packit 5ce601
		buf[2] = (ctx->val[2]<<6)|(ctx->val[3]);
Packit 5ce601
		RETERR(mem_tobuffer(ctx->target, buf, n));
Packit 5ce601
		if (ctx->length >= 0) {
Packit 5ce601
			if (n > ctx->length)
Packit 5ce601
				return (ISC_R_BADBASE64);
Packit 5ce601
			else
Packit 5ce601
				ctx->length -= n;
Packit 5ce601
		}
Packit 5ce601
		ctx->digits = 0;
Packit 5ce601
	}
Packit 5ce601
	return (ISC_R_SUCCESS);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static inline isc_result_t
Packit 5ce601
base64_decode_finish(base64_decode_ctx_t *ctx) {
Packit 5ce601
	if (ctx->length > 0)
Packit 5ce601
		return (ISC_R_UNEXPECTEDEND);
Packit 5ce601
	if (ctx->digits != 0)
Packit 5ce601
		return (ISC_R_BADBASE64);
Packit 5ce601
	return (ISC_R_SUCCESS);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
isc_result_t
Packit 5ce601
isc_base64_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
Packit 5ce601
	unsigned int before, after;
Packit 5ce601
	base64_decode_ctx_t ctx;
Packit 5ce601
	isc_textregion_t *tr;
Packit 5ce601
	isc_token_t token;
Packit 5ce601
	bool eol;
Packit 5ce601
Packit 5ce601
	REQUIRE(length >= -2);
Packit 5ce601
Packit 5ce601
	base64_decode_init(&ctx, length, target);
Packit 5ce601
Packit 5ce601
	before = isc_buffer_usedlength(target);
Packit 5ce601
	while (!ctx.seen_end && (ctx.length != 0)) {
Packit 5ce601
		unsigned int i;
Packit 5ce601
Packit 5ce601
		if (length > 0) {
Packit 5ce601
			eol = false;
Packit 5ce601
		} else {
Packit 5ce601
			eol = true;
Packit 5ce601
		}
Packit 5ce601
		RETERR(isc_lex_getmastertoken(lexer, &token,
Packit 5ce601
					      isc_tokentype_string, eol));
Packit 5ce601
		if (token.type != isc_tokentype_string) {
Packit 5ce601
			break;
Packit 5ce601
		}
Packit 5ce601
		tr = &token.value.as_textregion;
Packit 5ce601
		for (i = 0; i < tr->length; i++) {
Packit 5ce601
			RETERR(base64_decode_char(&ctx, tr->base[i]));
Packit 5ce601
		}
Packit 5ce601
	}
Packit 5ce601
	after = isc_buffer_usedlength(target);
Packit 5ce601
	if (ctx.length < 0 && !ctx.seen_end) {
Packit 5ce601
		isc_lex_ungettoken(lexer, &token);
Packit 5ce601
	}
Packit 5ce601
	RETERR(base64_decode_finish(&ctx));
Packit 5ce601
	if (length == -2 && before == after) {
Packit 5ce601
		return (ISC_R_UNEXPECTEDEND);
Packit 5ce601
	}
Packit 5ce601
	return (ISC_R_SUCCESS);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
isc_result_t
Packit 5ce601
isc_base64_decodestring(const char *cstr, isc_buffer_t *target) {
Packit 5ce601
	base64_decode_ctx_t ctx;
Packit 5ce601
Packit 5ce601
	base64_decode_init(&ctx, -1, target);
Packit 5ce601
	for (;;) {
Packit 5ce601
		int c = *cstr++;
Packit 5ce601
		if (c == '\0')
Packit 5ce601
			break;
Packit 5ce601
		if (c == ' ' || c == '\t' || c == '\n' || c== '\r')
Packit 5ce601
			continue;
Packit 5ce601
		RETERR(base64_decode_char(&ctx, c));
Packit 5ce601
	}
Packit 5ce601
	RETERR(base64_decode_finish(&ctx));
Packit 5ce601
	return (ISC_R_SUCCESS);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static isc_result_t
Packit 5ce601
str_totext(const char *source, isc_buffer_t *target) {
Packit 5ce601
	unsigned int l;
Packit 5ce601
	isc_region_t region;
Packit 5ce601
Packit 5ce601
	isc_buffer_availableregion(target, &region);
Packit 5ce601
	l = strlen(source);
Packit 5ce601
Packit 5ce601
	if (l > region.length)
Packit 5ce601
		return (ISC_R_NOSPACE);
Packit 5ce601
Packit 5ce601
	memmove(region.base, source, l);
Packit 5ce601
	isc_buffer_add(target, l);
Packit 5ce601
	return (ISC_R_SUCCESS);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static isc_result_t
Packit 5ce601
mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
Packit 5ce601
	isc_region_t tr;
Packit 5ce601
Packit 5ce601
	isc_buffer_availableregion(target, &tr);
Packit 5ce601
	if (length > tr.length)
Packit 5ce601
		return (ISC_R_NOSPACE);
Packit 5ce601
	memmove(tr.base, base, length);
Packit 5ce601
	isc_buffer_add(target, length);
Packit 5ce601
	return (ISC_R_SUCCESS);
Packit 5ce601
}