Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add feature for write/read data reusing the buffer passed in #7754

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 42 additions & 10 deletions examples/server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -532,15 +532,27 @@ int ServerEchoData(SSL* ssl, int clientfd, int echoData, int block,
return 0;
}

static void ServerRead(WOLFSSL* ssl, char* input, int inputLen)
static void ServerRead(WOLFSSL* ssl, char* input, int inputLen, byte inLine)
{
int ret, err;
char buffer[WOLFSSL_MAX_ERROR_SZ];

/* Read data */
do {
err = 0; /* reset error */
ret = SSL_read(ssl, input, inputLen);
if (inLine) {
byte largeBuffer[4096];
byte* outputPtr = NULL;

ret = wolfSSL_read_inline(ssl, largeBuffer, sizeof(largeBuffer),
(void**)&outputPtr, inputLen);
if (ret > 0) {
XMEMCPY(input, outputPtr, ret);
}
}
else {
ret = SSL_read(ssl, input, inputLen);
}
if (ret < 0) {
err = SSL_get_error(ssl, ret);

Expand Down Expand Up @@ -621,7 +633,8 @@ static void ServerRead(WOLFSSL* ssl, char* input, int inputLen)
}
}

static void ServerWrite(WOLFSSL* ssl, const char* output, int outputLen)
static void ServerWrite(WOLFSSL* ssl, const char* output, int outputLen,
byte inLine)
{
int ret, err;
int len;
Expand All @@ -636,7 +649,16 @@ static void ServerWrite(WOLFSSL* ssl, const char* output, int outputLen)

do {
err = 0; /* reset error */
ret = SSL_write(ssl, output, len);
if (inLine) {
byte largeBuffer[4096];

XMEMCPY(largeBuffer, output, outputLen);
ret = wolfSSL_write_inline(ssl, largeBuffer, outputLen,
sizeof(largeBuffer));
}
else {
ret = SSL_write(ssl, output, len);
}
if (ret <= 0) {
err = SSL_get_error(ssl, 0);

Expand Down Expand Up @@ -807,7 +829,7 @@ static void SetKeyShare(WOLFSSL* ssl, int onlyKeyShare, int useX25519,
/* 4. add the same message into Japanese section */
/* (will be translated later) */
/* 5. add printf() into suitable position of Usage() */
static const char* server_usage_msg[][65] = {
static const char* server_usage_msg[][66] = {
/* English */
{
" NOTE: All files relative to wolfSSL home dir\n", /* 0 */
Expand Down Expand Up @@ -980,10 +1002,12 @@ static const char* server_usage_msg[][65] = {
"--altPrivKey <file> Generate alternative signature with this key.\n",
/* 65 */
#endif
"--inline-io Does encrypt and decrypt inline with\n"
" wolfSSL_write and wolfSSL_read.\n", /* 66 */
"\n"
"For simpler wolfSSL TLS server examples, visit\n"
"https://github.com/wolfSSL/wolfssl-examples/tree/master/tls\n",
/* 66 */
/* 67 */
NULL,
},
#ifndef NO_MULTIBYTE_PRINT
Expand Down Expand Up @@ -1174,11 +1198,13 @@ static const char* server_usage_msg[][65] = {
"--altPrivKey <file> Generate alternative signature with this key.\n",
/* 65 */
#endif
"--inline-io Does encrypt and decrypt inline with\n"
" wolfSSL_write and wolfSSL_read.\n", /* 66 */
"\n"
"より簡単なwolfSSL TSL クライアントの例については"
"下記にアクセスしてください\n"
"https://github.com/wolfSSL/wolfssl-examples/tree/master/tls\n",
/* 66 */
/* 67 */
NULL,
},
#endif
Expand Down Expand Up @@ -1457,6 +1483,7 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
#ifdef WOLFSSL_DUAL_ALG_CERTS
{ "altPrivKey", 1, 267},
#endif
{ "inline-io", 0, 268},
{ 0, 0, 0 }
};
#endif
Expand Down Expand Up @@ -1626,6 +1653,7 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
char* altPrivKey = NULL;
int exitWithRet = 0;
int loadCertKeyIntoSSLObj = 0;
byte inLineIO = 0;

#ifdef HAVE_ENCRYPT_THEN_MAC
int disallowETM = 0;
Expand Down Expand Up @@ -2351,6 +2379,10 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
break;
#endif

case 268:
inLineIO = 1;
break;

case -1:
default:
Usage();
Expand Down Expand Up @@ -3620,7 +3652,7 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
#endif

if (echoData == 0 && throughput == 0) {
ServerRead(ssl, input, sizeof(input)-1);
ServerRead(ssl, input, sizeof(input)-1, inLineIO);
err = SSL_get_error(ssl, 0);
}

Expand Down Expand Up @@ -3739,11 +3771,11 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
write_msg = kHttpServerMsg;
write_msg_sz = (int)XSTRLEN(kHttpServerMsg);
}
ServerWrite(ssl, write_msg, write_msg_sz);
ServerWrite(ssl, write_msg, write_msg_sz, inLineIO);

#ifdef WOLFSSL_TLS13
if (updateKeysIVs || postHandAuth)
ServerRead(ssl, input, sizeof(input)-1);
ServerRead(ssl, input, sizeof(input)-1, 0);
#endif
}
else if (err == 0 || err == WOLFSSL_ERROR_ZERO_RETURN) {
Expand Down
82 changes: 73 additions & 9 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -10532,8 +10532,11 @@ static int wolfSSLReceive(WOLFSSL* ssl, byte* buf, word32 sz)
void ShrinkOutputBuffer(WOLFSSL* ssl)
{
WOLFSSL_MSG("Shrinking output buffer");
XFREE(ssl->buffers.outputBuffer.buffer - ssl->buffers.outputBuffer.offset,
ssl->heap, DYNAMIC_TYPE_OUT_BUFFER);

if (ssl->buffers.outputBuffer.dynamicFlag != WOLFSSL_EXTERNAL_IO_BUFFER) {
XFREE(ssl->buffers.outputBuffer.buffer -
ssl->buffers.outputBuffer.offset, ssl->heap, DYNAMIC_TYPE_OUT_BUFFER);
}
ssl->buffers.outputBuffer.buffer = ssl->buffers.outputBuffer.staticBuffer;
ssl->buffers.outputBuffer.bufferSize = STATIC_BUFFER_LEN;
ssl->buffers.outputBuffer.dynamicFlag = 0;
Expand Down Expand Up @@ -10563,10 +10566,12 @@ void ShrinkInputBuffer(WOLFSSL* ssl, int forcedFree)
usedLength);
}

ForceZero(ssl->buffers.inputBuffer.buffer,
ssl->buffers.inputBuffer.length);
XFREE(ssl->buffers.inputBuffer.buffer - ssl->buffers.inputBuffer.offset,
ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
if (ssl->buffers.inputBuffer.dynamicFlag != WOLFSSL_EXTERNAL_IO_BUFFER) {
ForceZero(ssl->buffers.inputBuffer.buffer,
ssl->buffers.inputBuffer.length);
XFREE(ssl->buffers.inputBuffer.buffer - ssl->buffers.inputBuffer.offset,
ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
}
ssl->buffers.inputBuffer.buffer = ssl->buffers.inputBuffer.staticBuffer;
ssl->buffers.inputBuffer.bufferSize = STATIC_BUFFER_LEN;
ssl->buffers.inputBuffer.dynamicFlag = 0;
Expand Down Expand Up @@ -10678,6 +10683,41 @@ byte* GetOutputBuffer(WOLFSSL* ssl)
}


/* sets the output buffer from an externally provided buffer */
int SetOutputBuffer(WOLFSSL* ssl, byte* buf, int bufSz)
{
if (ssl == NULL || buf == NULL) {
return BAD_FUNC_ARG;
}

/* data waiting to be sent, don't overwrite it */
if (ssl->buffers.outputBuffer.length > 0) {
return WANT_WRITE;
}

ssl->buffers.outputBuffer.dynamicFlag = WOLFSSL_EXTERNAL_IO_BUFFER;
ssl->buffers.outputBuffer.buffer = buf;
ssl->buffers.outputBuffer.bufferSize = bufSz;

return WOLFSSL_SUCCESS;
}


/* sets the input buffer from an externally provided buffer */
int SetInputBuffer(WOLFSSL* ssl, byte* buf, int bufSz)
{
if (ssl == NULL || buf == NULL) {
return BAD_FUNC_ARG;
}

ssl->buffers.inputBuffer.dynamicFlag = WOLFSSL_EXTERNAL_IO_BUFFER;
ssl->buffers.inputBuffer.buffer = buf;
ssl->buffers.inputBuffer.bufferSize = bufSz;

return WOLFSSL_SUCCESS;
}


/* Grow the output buffer */
static WC_INLINE int GrowOutputBuffer(WOLFSSL* ssl, int size)
{
Expand All @@ -10700,6 +10740,11 @@ static WC_INLINE int GrowOutputBuffer(WOLFSSL* ssl, int size)
align *= 2;
#endif

if (ssl->buffers.outputBuffer.dynamicFlag == WOLFSSL_EXTERNAL_IO_BUFFER) {
WOLFSSL_MSG("External output buffer provided was too small");
return BAD_FUNC_ARG;
}

if (! WC_SAFE_SUM_WORD32(ssl->buffers.outputBuffer.idx,
ssl->buffers.outputBuffer.length, newSz))
return BUFFER_E;
Expand Down Expand Up @@ -10781,6 +10826,11 @@ int GrowInputBuffer(WOLFSSL* ssl, int size, int usedLength)
return BAD_FUNC_ARG;
}

if (ssl->buffers.inputBuffer.dynamicFlag == WOLFSSL_EXTERNAL_IO_BUFFER) {
WOLFSSL_MSG("External input buffer provided was too small");
return BAD_FUNC_ARG;
}

tmp = (byte*)XMALLOC(size + usedLength + align,
ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
WOLFSSL_MSG("growing input buffer");
Expand Down Expand Up @@ -22706,6 +22756,12 @@ int BuildMessage(WOLFSSL* ssl, byte* output, int outSz, const byte* input,
}
#endif

/* move plan text data out of record headers way */
if (ssl->buffers.outputBuffer.dynamicFlag ==
WOLFSSL_EXTERNAL_IO_BUFFER) {
XMEMMOVE(output + args->headerSz + args->ivSz, input, inSz);
}

args->size = (word16)(args->sz - args->headerSz); /* include mac and digest */
AddRecordHeader(output, args->size, (byte)type, ssl, epochOrder);

Expand All @@ -22715,7 +22771,10 @@ int BuildMessage(WOLFSSL* ssl, byte* output, int outSz, const byte* input,
min(args->ivSz, MAX_IV_SZ));
args->idx += min(args->ivSz, MAX_IV_SZ);
}
XMEMCPY(output + args->idx, input, inSz);
if (ssl->buffers.outputBuffer.dynamicFlag !=
WOLFSSL_EXTERNAL_IO_BUFFER) {
XMEMCPY(output + args->idx, input, inSz);
}
args->idx += inSz;

ssl->options.buildMsgState = BUILD_MSG_HASH;
Expand Down Expand Up @@ -24628,7 +24687,7 @@ int SendData(WOLFSSL* ssl, const void* data, int sz)
}

/* process input data */
int ReceiveData(WOLFSSL* ssl, byte* output, int sz, int peek)
int ReceiveData(WOLFSSL* ssl, byte** output, int sz, int peek)
{
int size;

Expand Down Expand Up @@ -24775,7 +24834,12 @@ int ReceiveData(WOLFSSL* ssl, byte* output, int sz, int peek)

size = min(sz, (int)ssl->buffers.clearOutputBuffer.length);

XMEMCPY(output, ssl->buffers.clearOutputBuffer.buffer, size);
if (ssl->buffers.inputBuffer.dynamicFlag == WOLFSSL_EXTERNAL_IO_BUFFER) {
*output = ssl->buffers.clearOutputBuffer.buffer;
}
else {
XMEMCPY(*output, ssl->buffers.clearOutputBuffer.buffer, size);
}

if (peek == 0) {
ssl->buffers.clearOutputBuffer.length -= size;
Expand Down
Loading
Loading