Skip to contentSkip to conversation

base64: refactor l_base64_decode and validate bytes consumed

PATCH Applied James Prestwood 2 participants 3 messages Feb 17, 2022

lore
Share & tools
lore

James Prestwood

author
#0 lore raw

PATCH v2 1/2 base64: refactor l_base64_decode and validate bytes consumed

The way the base64 data was being validated was accurate, but included a second loop to iterate the padding. There was nothing wrong with this but for more concise/readable code the padding can be calculated and validated all after the first loop by incrementing pad_len when '=' is found rather than breaking out. The valid base64 character check did need a slight modification to also check that pad_len has not been incremented, which ensures there is no data after the padding (prior this was done by the second, pad checking, loop).

Then, after the loop, pad_len has been calculated and can be checked against what was expected based on the valid base64 characters found.

This refactoring also makes checking the bytes consumed much easier since the first loop iterates until the very end of the base64 data (including padding).

---
 ell/base64.c | 41 ++++++++++++++++++++++-------------------
 1 file changed, 22 insertions(+), 19 deletions(-)

v2:
* Fix issue in calculating the end of the base64 data (caused test-pem to fail). This was because whitespace was not taken into account which could exist between the base64 and padding. Instead the end of the base64 data is now saved when the first pad byte is found. This allows the second loop to use this as its end condition.

ell/base64.c +22 -19
index e430470..747d4d5 100644
@@ -27,56 +27,59 @@
 #include "utf8.h"
 #include "base64.h"
 #include "private.h"
+#include "useful.h"
+
+#include <stdio.h>
 
 LIB_EXPORT uint8_t *l_base64_decode(const char *in, size_t in_len,
 					size_t *n_written)
 {
 	const char *ptr, *in_end = in + in_len;
+	const char *base64_end = NULL;
 	uint8_t *out_buf, *out;
-	int base64_len = 0, pad_len;
+	int base64_len = 0, pad_len = 0;
 	uint16_t reg = 0;
 
-	for (ptr = in; ptr < in_end; ptr++)
+	for (ptr = in; ptr < in_end; ptr++) {
 		if (l_ascii_isspace(*ptr))
 			/* Whitespace */
 			continue;
-		else if (*ptr == '=')
+		else if (*ptr == '=') {
 			/* Final padding */
-			break;
-		else if (l_ascii_isalnum(*ptr) || *ptr == '+' || *ptr == '/')
+			if (!pad_len)
+				base64_end = ptr;
+
+			pad_len++;
+		} else if (!pad_len && (l_ascii_isalnum(*ptr) || *ptr == '+' ||
+					*ptr == '/'))
 			/* Base64 character */
 			base64_len++;
 		else
 			/* Bad character */
 			return NULL;
+	}
 
-	in_end = ptr;
+	if (ptr != in_end)
+		return NULL;
 
 	if ((base64_len & 3) == 1)
 		/* Invalid length */
 		return NULL;
 
-	pad_len = (4 - base64_len) & 3;
-	for (; ptr < in + in_len && pad_len; ptr++)
-		if (l_ascii_isspace(*ptr))
-			/* Whitespace */
-			continue;
-		else if (*ptr == '=')
-			/* Final padding */
-			pad_len--;
-		else
-			/* Bad character */
-			return NULL;
-	if (pad_len)
+	if (pad_len != align_len(base64_len, 4) - base64_len)
 		return NULL;
 
+	/* No padding */
+	if (!base64_end)
+		base64_end = ptr;
+
 	*n_written = base64_len * 3 / 4;
 	out_buf = l_malloc(*n_written);
 
 	out = out_buf;
 	base64_len = 0;
 
-	for (ptr = in; ptr < in_end; ptr++) {
+	for (ptr = in; ptr < base64_end; ptr++) {
 		if (l_ascii_isspace(*ptr))
 			/* Whitespace */
 			continue;
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 2/2 unit: add base64 failure cases

---
 unit/test-base64.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
unit/test-base64.c +46 -0
index 6e47c2b..2d5099d 100644
@@ -30,6 +30,7 @@
 
 struct base64_decode_test {
 	const char *input;
+	size_t input_size;
 	const uint8_t *output;
 	size_t output_size;
 };
@@ -81,6 +82,46 @@ static void test_base64_decode(const void *data)
 	l_free(decoded);
 }
 
+/* Length != string length */
+static const struct base64_decode_test error_1 = {
+	.input = "cGxlYXN1cmUu",
+	.input_size = 11
+};
+
+/* Length doesn't include pad */
+static const struct base64_decode_test error_2 = {
+	.input = "bGVhc3VyZS4=",
+	.input_size = 11,
+};
+
+/* Length doesn't include pad */
+static const struct base64_decode_test error_3 = {
+	.input = "ZWFzdXJlLg==",
+	.input_size = 10
+};
+
+/* Length correct, but data after padding */
+static const struct base64_decode_test error_4 = {
+	.input = "ZWFzdXJlLg==bG",
+	.input_size = 14
+};
+
+/* Only pad */
+static const struct base64_decode_test error_5 = {
+	.input = "==",
+	.input_size = 2
+};
+
+static void test_base64_error(const void *data)
+{
+	const struct base64_decode_test *test = data;
+	uint8_t *decoded;
+	size_t decoded_size;
+
+	decoded = l_base64_decode(test->input, test->input_size, &decoded_size);
+	assert(!decoded);
+}
+
 struct base64_encode_test {
 	const char *input;
 	const char *output;
@@ -133,6 +174,11 @@ int main(int argc, char *argv[])
 	l_test_add("base64/decode/test2", test_base64_decode, &decode_2);
 	l_test_add("base64/decode/test3", test_base64_decode, &decode_3);
 	l_test_add("base64/decode/test4", test_base64_decode, &decode_4);
+	l_test_add("base64/decode/test5", test_base64_error, &error_1);
+	l_test_add("base64/decode/test6", test_base64_error, &error_2);
+	l_test_add("base64/decode/test7", test_base64_error, &error_3);
+	l_test_add("base64/decode/test8", test_base64_error, &error_4);
+	l_test_add("base64/decode/test9", test_base64_error, &error_5);
 
 	l_test_add("base64/encode/test1", test_base64_encode, &encode_1);
 	l_test_add("base64/encode/test2", test_base64_encode, &encode_2);
Signature · 1 line
2.34.1
Applied
#2 lore raw
In reply to James Prestwood

Hi James,

Quoted 19 lines · James Prestwood

The way the base64 data was being validated was accurate, but included a second loop to iterate the padding. There was nothing wrong with this but for more concise/readable code the padding can be calculated and validated all after the first loop by incrementing pad_len when '=' is found rather than breaking out. The valid base64 character check did need a slight modification to also check that pad_len has not been incremented, which ensures there is no data after the padding (prior this was done by the second, pad checking, loop).

Then, after the loop, pad_len has been calculated and can be checked against what was expected based on the valid base64 characters found.

This refactoring also makes checking the bytes consumed much easier since the first loop iterates until the very end of the base64 data (including padding).

---
  ell/base64.c | 41 ++++++++++++++++++++++-------------------
  1 file changed, 22 insertions(+), 19 deletions(-)
Quoted 7 lines

v2:
* Fix issue in calculating the end of the base64 data (caused test-pem to fail). This was because whitespace was not taken into account which could exist between the base64 and padding. Instead the end of the base64 data is now saved when the first pad byte is found. This allows the second loop to use this as its end condition.

Applied, thanks.

Regards,
-Denis