aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2020-11-30 19:15:41 +0000
committerDaniel Eklöf <daniel@ekloef.se>2020-11-30 19:15:41 +0000
commit282ca891681460ec43fc66a6cc9886001b72da9c (patch)
tree47073b61713e19761f733ee121da3243b879c4df
parentMerge branch 'simplify-config-struct' (diff)
parentbase64: only allow padding bytes at the end of a string when decoding (diff)
Merge branch 'base64-padding-fix'
-rw-r--r--base64.c23
1 files changed, 17 insertions, 6 deletions
diff --git a/base64.c b/base64.c
index da28e614..c0dfc807 100644
--- a/base64.c
+++ b/base64.c
@@ -12,8 +12,8 @@
#include "log.h"
enum {
- P = 0, // Padding byte (=)
- I = 128, // Invalid byte ([^A-Za-z0-9+/=])
+ P = 1 << 6, // Padding byte (=)
+ I = 1 << 7, // Invalid byte ([^A-Za-z0-9+/=])
};
static const uint8_t reverse_lookup[256] = {
@@ -63,10 +63,16 @@ base64_decode(const char *s)
unsigned c = reverse_lookup[(unsigned char)s[i + 2]];
unsigned d = reverse_lookup[(unsigned char)s[i + 3]];
- if (unlikely((a | b | c | d) & I)) {
- free(ret);
- errno = EINVAL;
- return NULL;
+ unsigned u = a | b | c | d;
+ if (unlikely(u & I))
+ goto invalid;
+
+ if (unlikely(u & P)) {
+ if (unlikely(i + 4 != len || (a | b) & P || (c & P && !(d & P))))
+ goto invalid;
+
+ c &= 63;
+ d &= 63;
}
uint32_t v = a << 18 | b << 12 | c << 6 | d << 0;
@@ -82,6 +88,11 @@ base64_decode(const char *s)
ret[len / 4 * 3] = '\0';
return ret;
+
+invalid:
+ free(ret);
+ errno = EINVAL;
+ return NULL;
}
char *