diff --git a/wolfcrypt/src/coding.c b/wolfcrypt/src/coding.c index 822c5e48f..ec1bc4b13 100644 --- a/wolfcrypt/src/coding.c +++ b/wolfcrypt/src/coding.c @@ -117,23 +117,31 @@ static WC_INLINE int Base64_SkipNewline(const byte* in, word32 *inLen, word32 *o { word32 len = *inLen; word32 j = *outJ; - if (len && (in[j] == ' ' || in[j] == '\r' || in[j] == '\n')) { - byte endLine = in[j++]; + byte curChar = in[j]; + while (len && curChar == ' ') { + /* skip whitespace in the middle or end of line */ + curChar = in[++j]; len--; - while (len && endLine == ' ') { /* allow trailing whitespace */ - endLine = in[j++]; - len--; - } - if (endLine == '\r') { + } + if (len && (curChar == '\r' || curChar == '\n')) { + j++; + len--; + if (curChar == '\r') { if (len) { - endLine = in[j++]; + curChar = in[j++]; len--; } } - if (endLine != '\n') { + if (curChar != '\n') { WOLFSSL_MSG("Bad end of line in Base64 Decode"); return ASN_INPUT_E; } + curChar = in[j]; + } + while (len && curChar == ' ') { + /* skip whitespace at beginning of line */ + curChar = in[++j]; + len--; } if (!len) { return BUFFER_E; diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 5a793dccd..b74e2de90 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -1652,6 +1652,7 @@ WOLFSSL_TEST_SUBROUTINE int base64_test(void) int ret; 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 good_spaces[] = " A + G d \0"; byte out[128]; word32 outLen; #ifdef WOLFSSL_BASE64_ENCODE @@ -1660,9 +1661,9 @@ WOLFSSL_TEST_SUBROUTINE int base64_test(void) byte longData[79] = { 0 }; WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A="; #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 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 badChar[] = ",-.:;<=>?@[\\]^_`"; byte goodChar[] = @@ -1687,6 +1688,10 @@ WOLFSSL_TEST_SUBROUTINE int base64_test(void) return -1235; if (outLen != 64 / 4 * 3) return -1236; + outLen = sizeof(out); + ret = Base64_Decode(good_spaces, sizeof(good_spaces), out, &outLen); + if (ret != 0) + return -1201; /* Bad parameters. */ outLen = 1;