diff --git a/src/bio.c b/src/bio.c index 5c636d887..2d740de77 100644 --- a/src/bio.c +++ b/src/bio.c @@ -278,7 +278,7 @@ static int wolfSSL_BIO_BASE64_write(WOLFSSL_BIO* bio, const void* data, } if (ret != WOLFSSL_FATAL_ERROR) { - ret = (int)*outLen; + ret = (int) inLen; XMEMCPY(out, tmp, *outLen); } @@ -433,6 +433,7 @@ static int wolfSSL_BIO_MEMORY_write(WOLFSSL_BIO* bio, const void* data, int wolfSSL_BIO_write(WOLFSSL_BIO* bio, const void* data, int len) { int ret = 0; + int retB64 = 0; WOLFSSL_BIO* front = bio; void* frmt = NULL; word32 frmtSz = 0; @@ -500,8 +501,8 @@ int wolfSSL_BIO_write(WOLFSSL_BIO* bio, const void* data, int len) if (ret >= 0) { /* change so that data is formated buffer */ - ret = wolfSSL_BIO_BASE64_write(bio, data, (word32)len, - (byte*)frmt, &frmtSz); + retB64 = wolfSSL_BIO_BASE64_write(bio, data, (word32)len, + (byte*)frmt, &frmtSz); data = frmt; len = frmtSz; } @@ -549,7 +550,10 @@ int wolfSSL_BIO_write(WOLFSSL_BIO* bio, const void* data, int len) (const char*)data, 0, 0, ret); } - return ret; + if (retB64 != 0) + return retB64; + else + return ret; } diff --git a/tests/api.c b/tests/api.c index fa53eef67..575a92598 100644 --- a/tests/api.c +++ b/tests/api.c @@ -22982,7 +22982,7 @@ static void test_wolfSSL_BIO_write(void) AssertNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem()))); /* now should convert to base64 then write to memory */ - AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25); + AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg)); BIO_flush(bio); /* test BIO chain */ @@ -22997,7 +22997,7 @@ static void test_wolfSSL_BIO_write(void) AssertIntEQ(XMEMCMP(out, expected, sz), 0); /* write then read should return the same message */ - AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25); + AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg)); sz = sizeof(out); XMEMSET(out, 0, sz); AssertIntEQ(BIO_read(bio, out, sz), 16); @@ -23005,7 +23005,7 @@ static void test_wolfSSL_BIO_write(void) /* now try encoding with no line ending */ BIO_set_flags(bio64, BIO_FLAG_BASE64_NO_NL); - AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 24); + AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg)); BIO_flush(bio); sz = sizeof(out); XMEMSET(out, 0, sz); @@ -23019,8 +23019,8 @@ static void test_wolfSSL_BIO_write(void) AssertNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64)); AssertNotNull(BIO_push(bio64, BIO_new(BIO_s_mem()))); - /* now should convert to base64(x2) when stored and then decode with read */ - AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 37); + /* now should convert to base64 when stored and then decode with read */ + AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25); BIO_flush(bio); sz = sizeof(out); XMEMSET(out, 0, sz); diff --git a/wolfcrypt/src/coding.c b/wolfcrypt/src/coding.c index cee699575..88b447a08 100644 --- a/wolfcrypt/src/coding.c +++ b/wolfcrypt/src/coding.c @@ -130,7 +130,7 @@ int Base64_Decode(const byte* in, word32 inLen, byte* out, word32* outLen) } } } - +/* If the output buffer has a room for an extra byte, add a null terminator */ if (out && *outLen > i) out[i]= '\0'; @@ -324,7 +324,7 @@ static int DoBase64_Encode(const byte* in, word32 inLen, byte* out, if (i != outSz && escaped != 1 && ret == 0) return ASN_INPUT_E; - +/* If the output buffer has a room for an extra byte, add a null terminator */ if (out && *outLen > i) out[i]= '\0';