xref: /openbsd/usr.bin/dig/lib/isc/base64.c (revision 1fb015a8)
15185a700Sflorian /*
25185a700Sflorian  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
35185a700Sflorian  *
45185a700Sflorian  * Permission to use, copy, modify, and/or distribute this software for any
55185a700Sflorian  * purpose with or without fee is hereby granted, provided that the above
65185a700Sflorian  * copyright notice and this permission notice appear in all copies.
75185a700Sflorian  *
85185a700Sflorian  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
95185a700Sflorian  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
105185a700Sflorian  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
115185a700Sflorian  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
125185a700Sflorian  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
135185a700Sflorian  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
145185a700Sflorian  * PERFORMANCE OF THIS SOFTWARE.
155185a700Sflorian  */
165185a700Sflorian 
17*1fb015a8Sflorian /* $Id: base64.c,v 1.7 2020/09/14 08:40:44 florian Exp $ */
185185a700Sflorian 
195185a700Sflorian /*! \file */
205185a700Sflorian 
214465bcfbSjsg #include <string.h>
225185a700Sflorian 
235185a700Sflorian #include <isc/base64.h>
245185a700Sflorian #include <isc/buffer.h>
254465bcfbSjsg #include <isc/region.h>
265185a700Sflorian #include <isc/util.h>
275185a700Sflorian 
285185a700Sflorian #define RETERR(x) do { \
295185a700Sflorian 	isc_result_t _r = (x); \
305185a700Sflorian 	if (_r != ISC_R_SUCCESS) \
315185a700Sflorian 		return (_r); \
325185a700Sflorian 	} while (0)
335185a700Sflorian 
345185a700Sflorian /*@{*/
355185a700Sflorian 
365185a700Sflorian static const char base64[] =
375185a700Sflorian 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
385185a700Sflorian /*@}*/
395185a700Sflorian 
405185a700Sflorian isc_result_t
isc_base64_totext(isc_region_t * source,int wordlength,const char * wordbreak,isc_buffer_t * target)415185a700Sflorian isc_base64_totext(isc_region_t *source, int wordlength,
425185a700Sflorian 		  const char *wordbreak, isc_buffer_t *target)
435185a700Sflorian {
445185a700Sflorian 	char buf[5];
455185a700Sflorian 	unsigned int loops = 0;
465185a700Sflorian 
475185a700Sflorian 	if (wordlength < 4)
485185a700Sflorian 		wordlength = 4;
495185a700Sflorian 
505185a700Sflorian 	memset(buf, 0, sizeof(buf));
515185a700Sflorian 	while (source->length > 2) {
525185a700Sflorian 		buf[0] = base64[(source->base[0]>>2)&0x3f];
535185a700Sflorian 		buf[1] = base64[((source->base[0]<<4)&0x30)|
545185a700Sflorian 				((source->base[1]>>4)&0x0f)];
555185a700Sflorian 		buf[2] = base64[((source->base[1]<<2)&0x3c)|
565185a700Sflorian 				((source->base[2]>>6)&0x03)];
575185a700Sflorian 		buf[3] = base64[source->base[2]&0x3f];
58873f12b9Sflorian 		RETERR(isc_str_tobuffer(buf, target));
595185a700Sflorian 		isc_region_consume(source, 3);
605185a700Sflorian 
615185a700Sflorian 		loops++;
625185a700Sflorian 		if (source->length != 0 &&
635185a700Sflorian 		    (int)((loops + 1) * 4) >= wordlength)
645185a700Sflorian 		{
655185a700Sflorian 			loops = 0;
66873f12b9Sflorian 			RETERR(isc_str_tobuffer(wordbreak, target));
675185a700Sflorian 		}
685185a700Sflorian 	}
695185a700Sflorian 	if (source->length == 2) {
705185a700Sflorian 		buf[0] = base64[(source->base[0]>>2)&0x3f];
715185a700Sflorian 		buf[1] = base64[((source->base[0]<<4)&0x30)|
725185a700Sflorian 				((source->base[1]>>4)&0x0f)];
735185a700Sflorian 		buf[2] = base64[((source->base[1]<<2)&0x3c)];
745185a700Sflorian 		buf[3] = '=';
75873f12b9Sflorian 		RETERR(isc_str_tobuffer(buf, target));
765185a700Sflorian 		isc_region_consume(source, 2);
775185a700Sflorian 	} else if (source->length == 1) {
785185a700Sflorian 		buf[0] = base64[(source->base[0]>>2)&0x3f];
795185a700Sflorian 		buf[1] = base64[((source->base[0]<<4)&0x30)];
805185a700Sflorian 		buf[2] = buf[3] = '=';
81873f12b9Sflorian 		RETERR(isc_str_tobuffer(buf, target));
825185a700Sflorian 		isc_region_consume(source, 1);
835185a700Sflorian 	}
845185a700Sflorian 	return (ISC_R_SUCCESS);
855185a700Sflorian }
865185a700Sflorian 
875185a700Sflorian /*%
885185a700Sflorian  * State of a base64 decoding process in progress.
895185a700Sflorian  */
905185a700Sflorian typedef struct {
915185a700Sflorian 	int length;		/*%< Desired length of binary data or -1 */
925185a700Sflorian 	isc_buffer_t *target;	/*%< Buffer for resulting binary data */
935185a700Sflorian 	int digits;		/*%< Number of buffered base64 digits */
94*1fb015a8Sflorian 	int seen_end;	/*%< True if "=" end marker seen */
955185a700Sflorian 	int val[4];
965185a700Sflorian } base64_decode_ctx_t;
975185a700Sflorian 
985185a700Sflorian static inline void
base64_decode_init(base64_decode_ctx_t * ctx,int length,isc_buffer_t * target)995185a700Sflorian base64_decode_init(base64_decode_ctx_t *ctx, int length, isc_buffer_t *target)
1005185a700Sflorian {
1015185a700Sflorian 	ctx->digits = 0;
102*1fb015a8Sflorian 	ctx->seen_end = 0;
1035185a700Sflorian 	ctx->length = length;
1045185a700Sflorian 	ctx->target = target;
1055185a700Sflorian }
1065185a700Sflorian 
1075185a700Sflorian static inline isc_result_t
base64_decode_char(base64_decode_ctx_t * ctx,int c)1085185a700Sflorian base64_decode_char(base64_decode_ctx_t *ctx, int c) {
1095185a700Sflorian 	const char *s;
1105185a700Sflorian 
1115185a700Sflorian 	if (ctx->seen_end)
1125185a700Sflorian 		return (ISC_R_BADBASE64);
1135185a700Sflorian 	if ((s = strchr(base64, c)) == NULL)
1145185a700Sflorian 		return (ISC_R_BADBASE64);
1155185a700Sflorian 	ctx->val[ctx->digits++] = (int)(s - base64);
1165185a700Sflorian 	if (ctx->digits == 4) {
1175185a700Sflorian 		int n;
1185185a700Sflorian 		unsigned char buf[3];
1195185a700Sflorian 		if (ctx->val[0] == 64 || ctx->val[1] == 64)
1205185a700Sflorian 			return (ISC_R_BADBASE64);
1215185a700Sflorian 		if (ctx->val[2] == 64 && ctx->val[3] != 64)
1225185a700Sflorian 			return (ISC_R_BADBASE64);
1235185a700Sflorian 		/*
1245185a700Sflorian 		 * Check that bits that should be zero are.
1255185a700Sflorian 		 */
1265185a700Sflorian 		if (ctx->val[2] == 64 && (ctx->val[1] & 0xf) != 0)
1275185a700Sflorian 			return (ISC_R_BADBASE64);
1285185a700Sflorian 		/*
1295185a700Sflorian 		 * We don't need to test for ctx->val[2] != 64 as
1305185a700Sflorian 		 * the bottom two bits of 64 are zero.
1315185a700Sflorian 		 */
1325185a700Sflorian 		if (ctx->val[3] == 64 && (ctx->val[2] & 0x3) != 0)
1335185a700Sflorian 			return (ISC_R_BADBASE64);
1345185a700Sflorian 		n = (ctx->val[2] == 64) ? 1 :
1355185a700Sflorian 			(ctx->val[3] == 64) ? 2 : 3;
1365185a700Sflorian 		if (n != 3) {
137*1fb015a8Sflorian 			ctx->seen_end = 1;
1385185a700Sflorian 			if (ctx->val[2] == 64)
1395185a700Sflorian 				ctx->val[2] = 0;
1405185a700Sflorian 			if (ctx->val[3] == 64)
1415185a700Sflorian 				ctx->val[3] = 0;
1425185a700Sflorian 		}
1435185a700Sflorian 		buf[0] = (ctx->val[0]<<2)|(ctx->val[1]>>4);
1445185a700Sflorian 		buf[1] = (ctx->val[1]<<4)|(ctx->val[2]>>2);
1455185a700Sflorian 		buf[2] = (ctx->val[2]<<6)|(ctx->val[3]);
146637d8eb6Sflorian 		RETERR(isc_mem_tobuffer(ctx->target, buf, n));
1475185a700Sflorian 		if (ctx->length >= 0) {
1485185a700Sflorian 			if (n > ctx->length)
1495185a700Sflorian 				return (ISC_R_BADBASE64);
1505185a700Sflorian 			else
1515185a700Sflorian 				ctx->length -= n;
1525185a700Sflorian 		}
1535185a700Sflorian 		ctx->digits = 0;
1545185a700Sflorian 	}
1555185a700Sflorian 	return (ISC_R_SUCCESS);
1565185a700Sflorian }
1575185a700Sflorian 
1585185a700Sflorian static inline isc_result_t
base64_decode_finish(base64_decode_ctx_t * ctx)1595185a700Sflorian base64_decode_finish(base64_decode_ctx_t *ctx) {
1605185a700Sflorian 	if (ctx->length > 0)
1615185a700Sflorian 		return (ISC_R_UNEXPECTEDEND);
1625185a700Sflorian 	if (ctx->digits != 0)
1635185a700Sflorian 		return (ISC_R_BADBASE64);
1645185a700Sflorian 	return (ISC_R_SUCCESS);
1655185a700Sflorian }
1665185a700Sflorian 
1675185a700Sflorian isc_result_t
isc_base64_decodestring(const char * cstr,isc_buffer_t * target)1685185a700Sflorian isc_base64_decodestring(const char *cstr, isc_buffer_t *target) {
1695185a700Sflorian 	base64_decode_ctx_t ctx;
1705185a700Sflorian 
1715185a700Sflorian 	base64_decode_init(&ctx, -1, target);
1725185a700Sflorian 	for (;;) {
1735185a700Sflorian 		int c = *cstr++;
1745185a700Sflorian 		if (c == '\0')
1755185a700Sflorian 			break;
1765185a700Sflorian 		if (c == ' ' || c == '\t' || c == '\n' || c== '\r')
1775185a700Sflorian 			continue;
1785185a700Sflorian 		RETERR(base64_decode_char(&ctx, c));
1795185a700Sflorian 	}
1805185a700Sflorian 	RETERR(base64_decode_finish(&ctx));
1815185a700Sflorian 	return (ISC_R_SUCCESS);
1825185a700Sflorian }
183