mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-08-03 12:44:45 +02:00
Merge pull request #2506 from tmael/bio_mem_base64
Fix for BIO and base64 encoding/decoding
This commit is contained in:
24
src/bio.c
24
src/bio.c
@@ -869,20 +869,34 @@ size_t wolfSSL_BIO_ctrl_pending(WOLFSSL_BIO *bio)
|
|||||||
|
|
||||||
long wolfSSL_BIO_get_mem_ptr(WOLFSSL_BIO *bio, WOLFSSL_BUF_MEM **ptr)
|
long wolfSSL_BIO_get_mem_ptr(WOLFSSL_BIO *bio, WOLFSSL_BUF_MEM **ptr)
|
||||||
{
|
{
|
||||||
|
WOLFSSL_BIO* front = bio;
|
||||||
|
long ret = WOLFSSL_FAILURE;
|
||||||
|
|
||||||
WOLFSSL_ENTER("wolfSSL_BIO_get_mem_ptr");
|
WOLFSSL_ENTER("wolfSSL_BIO_get_mem_ptr");
|
||||||
|
|
||||||
if (bio == NULL || ptr == NULL) {
|
if (bio == NULL || ptr == NULL) {
|
||||||
return WOLFSSL_FAILURE;
|
return WOLFSSL_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bio->type != WOLFSSL_BIO_MEMORY) {
|
/* start at end and work backwards to find a memory BIO in the BIO chain */
|
||||||
WOLFSSL_MSG("BIO is not memory buffer type");
|
while ((bio != NULL) && (bio->next != NULL)) {
|
||||||
return SSL_FAILURE;
|
bio = bio->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
*ptr = bio->mem_buf;
|
while (bio != NULL) {
|
||||||
|
|
||||||
return SSL_SUCCESS;
|
if (bio->type == WOLFSSL_BIO_MEMORY) {
|
||||||
|
*ptr = bio->mem_buf;
|
||||||
|
ret = WOLFSSL_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bio == front) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
bio = bio->prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
WOLFSSL_API long wolfSSL_BIO_int_ctrl(WOLFSSL_BIO *bp, int cmd, long larg, int iarg)
|
WOLFSSL_API long wolfSSL_BIO_int_ctrl(WOLFSSL_BIO *bp, int cmd, long larg, int iarg)
|
||||||
|
@@ -22974,6 +22974,7 @@ static void test_wolfSSL_BIO_write(void)
|
|||||||
char msg[] = "conversion test";
|
char msg[] = "conversion test";
|
||||||
char out[40];
|
char out[40];
|
||||||
char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
|
char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
|
||||||
|
BUF_MEM* buf = NULL;
|
||||||
|
|
||||||
printf(testingFmt, "wolfSSL_BIO_write()");
|
printf(testingFmt, "wolfSSL_BIO_write()");
|
||||||
|
|
||||||
@@ -22983,6 +22984,12 @@ static void test_wolfSSL_BIO_write(void)
|
|||||||
/* now should convert to base64 then write to memory */
|
/* now should convert to base64 then write to memory */
|
||||||
AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
|
AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
|
||||||
BIO_flush(bio);
|
BIO_flush(bio);
|
||||||
|
|
||||||
|
/* test BIO chain */
|
||||||
|
AssertIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
|
||||||
|
AssertNotNull(buf);
|
||||||
|
AssertIntEQ(buf->length, 25);
|
||||||
|
|
||||||
AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
|
AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
|
||||||
sz = sizeof(out);
|
sz = sizeof(out);
|
||||||
XMEMSET(out, 0, sz);
|
XMEMSET(out, 0, sz);
|
||||||
|
@@ -130,6 +130,10 @@ int Base64_Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (out && *outLen > i)
|
||||||
|
out[i]= '\0';
|
||||||
|
|
||||||
*outLen = i;
|
*outLen = i;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -321,9 +325,14 @@ static int DoBase64_Encode(const byte* in, word32 inLen, byte* out,
|
|||||||
if (i != outSz && escaped != 1 && ret == 0)
|
if (i != outSz && escaped != 1 && ret == 0)
|
||||||
return ASN_INPUT_E;
|
return ASN_INPUT_E;
|
||||||
|
|
||||||
|
if (out && *outLen > i)
|
||||||
|
out[i]= '\0';
|
||||||
|
|
||||||
*outLen = i;
|
*outLen = i;
|
||||||
if(ret == 0)
|
|
||||||
|
if (ret == 0)
|
||||||
return getSzOnly ? LENGTH_ONLY_E : 0;
|
return getSzOnly ? LENGTH_ONLY_E : 0;
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user