Allow parsing spaces in Base64_SkipNewline

This commit is contained in:
Eric Blankenhorn
2021-04-28 10:30:16 -05:00
parent 385e0bedaa
commit cdede0515c
2 changed files with 24 additions and 15 deletions

View File

@ -117,23 +117,31 @@ static WC_INLINE int Base64_SkipNewline(const byte* in, word32 *inLen, word32 *o
{ {
word32 len = *inLen; word32 len = *inLen;
word32 j = *outJ; word32 j = *outJ;
if (len && (in[j] == ' ' || in[j] == '\r' || in[j] == '\n')) { byte curChar = in[j];
byte endLine = in[j++]; while (len && curChar == ' ') {
/* skip whitespace in the middle or end of line */
curChar = in[++j];
len--; len--;
while (len && endLine == ' ') { /* allow trailing whitespace */ }
endLine = in[j++]; if (len && (curChar == '\r' || curChar == '\n')) {
len--; j++;
} len--;
if (endLine == '\r') { if (curChar == '\r') {
if (len) { if (len) {
endLine = in[j++]; curChar = in[j++];
len--; len--;
} }
} }
if (endLine != '\n') { if (curChar != '\n') {
WOLFSSL_MSG("Bad end of line in Base64 Decode"); WOLFSSL_MSG("Bad end of line in Base64 Decode");
return ASN_INPUT_E; return ASN_INPUT_E;
} }
curChar = in[j];
}
while (len && curChar == ' ') {
/* skip whitespace at beginning of line */
curChar = in[++j];
len--;
} }
if (!len) { if (!len) {
return BUFFER_E; return BUFFER_E;

View File

@ -176,10 +176,6 @@ _Pragma("GCC diagnostic ignored \"-Wunused-function\"");
__android_log_print(ANDROID_LOG_DEBUG, "TAG", __VA_ARGS__) __android_log_print(ANDROID_LOG_DEBUG, "TAG", __VA_ARGS__)
#define fprintf(fp, ...) \ #define fprintf(fp, ...) \
__android_log_print(ANDROID_LOG_DEBUG, "TAG", __VA_ARGS__) __android_log_print(ANDROID_LOG_DEBUG, "TAG", __VA_ARGS__)
#elif defined(WOLFSSL_DEOS)
#include <printx.h>
#undef printf
#define printf printx
#else #else
#ifdef XMALLOC_USER #ifdef XMALLOC_USER
#include <stdlib.h> /* we're using malloc / free direct here */ #include <stdlib.h> /* we're using malloc / free direct here */
@ -1652,6 +1648,7 @@ WOLFSSL_TEST_SUBROUTINE int base64_test(void)
int ret; int ret;
WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0"; WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n"; WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
WOLFSSL_SMALL_STACK_STATIC const byte good_spaces[] = " A + G d \0";
byte out[128]; byte out[128];
word32 outLen; word32 outLen;
#ifdef WOLFSSL_BASE64_ENCODE #ifdef WOLFSSL_BASE64_ENCODE
@ -1660,9 +1657,9 @@ WOLFSSL_TEST_SUBROUTINE int base64_test(void)
byte longData[79] = { 0 }; byte longData[79] = { 0 };
WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A="; WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
#endif #endif
WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA Gdj="; WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA!Gdj=";
WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj="; WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd AA"; WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd!AA";
WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A"; WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`"; WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
byte goodChar[] = byte goodChar[] =
@ -1687,6 +1684,10 @@ WOLFSSL_TEST_SUBROUTINE int base64_test(void)
return -1235; return -1235;
if (outLen != 64 / 4 * 3) if (outLen != 64 / 4 * 3)
return -1236; return -1236;
outLen = sizeof(out);
ret = Base64_Decode(good_spaces, sizeof(good_spaces), out, &outLen);
if (ret != 0)
return -1201;
/* Bad parameters. */ /* Bad parameters. */
outLen = 1; outLen = 1;