Skip to content
Merged
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
20 changes: 15 additions & 5 deletions wolfcrypt/src/pkcs7.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,22 @@ static void wc_PKCS7_ResetStream(wc_PKCS7* pkcs7)
XFREE(pkcs7->stream->tag, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
XFREE(pkcs7->stream->nonce, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
XFREE(pkcs7->stream->buffer, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
/* stream->bufferPt holds the AuthEnvelopedData encryptedContent
* buffer across WANT_READ re-entries. wc_PKCS7_DecodeAuthEnveloped
* Data() only frees it on its own error paths; if the stream is torn
* down while a decode is still pending (e.g. wc_PKCS7_Free() called
* after a WANT_READ), it must be freed here or it leaks. */
XFREE(pkcs7->stream->bufferPt, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
/* stream->key is always allocated with MAX_ENCRYPTED_KEY_SZ */
if (pkcs7->stream->key != NULL)
ForceZero(pkcs7->stream->key, MAX_ENCRYPTED_KEY_SZ);
XFREE(pkcs7->stream->key, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
pkcs7->stream->aad = NULL;
pkcs7->stream->tag = NULL;
pkcs7->stream->nonce = NULL;
pkcs7->stream->buffer = NULL;
pkcs7->stream->key = NULL;
pkcs7->stream->aad = NULL;
pkcs7->stream->tag = NULL;
pkcs7->stream->nonce = NULL;
pkcs7->stream->buffer = NULL;
pkcs7->stream->bufferPt = NULL;
pkcs7->stream->key = NULL;

/* reset values, note that content and tmpCert are saved */
pkcs7->stream->maxLen = 0;
Expand Down Expand Up @@ -16424,6 +16431,9 @@ int wc_PKCS7_DecodeAuthEnvelopedData(wc_PKCS7* pkcs7, byte* in,
ForceZero(encryptedContent, (word32)encryptedContentSz);
XFREE(encryptedContent, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
encryptedContent = NULL;
#ifndef NO_PKCS7_STREAM
pkcs7->stream->bufferPt = NULL;
#endif
ForceZero(decryptedKey, MAX_ENCRYPTED_KEY_SZ);
XFREE(decryptedKey, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
decryptedKey = NULL;
Expand Down
48 changes: 33 additions & 15 deletions wolfcrypt/src/wc_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -1550,34 +1550,52 @@ char* wc_strsep(char **stringp, const char *delim)
#ifdef USE_WOLF_STRLCPY
size_t wc_strlcpy(char *dst, const char *src, size_t dstSize)
{
size_t i;
size_t i = 0;

if (!dstSize)
return 0;
if (dstSize != 0) {
/* Always have to leave a space for NULL */
for (; i < (dstSize - 1) && *src != '\0'; i++) {
*dst++ = *src++;
}
*dst = '\0';
}

/* Always have to leave a space for NULL */
for (i = 0; i < (dstSize - 1) && *src != '\0'; i++) {
*dst++ = *src++;
/* strlcpy() returns the length of src, not the number of bytes copied, so
* that a caller can detect truncation with (ret >= dstSize). Walk whatever
* did not fit -- src already points at the first byte not copied, and at
* the whole string when dstSize was 0 (which writes nothing). */
while (*src != '\0') {
i++;
src++;
}
*dst = '\0';

return i; /* return length without NULL */
return i; /* length of src, excluding the NULL */
}
#endif /* USE_WOLF_STRLCPY */

#ifdef USE_WOLF_STRLCAT
size_t wc_strlcat(char *dst, const char *src, size_t dstSize)
{
size_t dstLen;
size_t dstLen = 0;

if (!dstSize)
return 0;

dstLen = XSTRLEN(dst);
/* Find the end of dst without going past dstSize. XSTRLEN() would run off
* the end of a dst that holds no NUL within dstSize -- the very case this
* bound exists to contain. */
while (dstLen < dstSize && dst[dstLen] != '\0') {
dstLen++;
}

if (dstSize < dstLen)
return dstLen + XSTRLEN(src);
if (dstLen == dstSize) {
/* No NUL within dstSize: the length of dst is taken to be dstSize,
* nothing is appended, and dst is left un-terminated because there is
* no room for the NUL. Only reachable when dstSize is wrong or dst is
* not a C string; returning here is what stops the append from running
* off the end. */
return dstSize + XSTRLEN(src);
}

/* Total length attempted: the initial length of dst plus the length of
* src, which is what wc_strlcpy() returns. */
return dstLen + wc_strlcpy(dst + dstLen, src, dstSize - dstLen);
}
#endif /* USE_WOLF_STRLCAT */
Expand Down
Loading