Skip to content
Merged
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
15 changes: 14 additions & 1 deletion wolfcrypt/src/asn.c
Original file line number Diff line number Diff line change
Expand Up @@ -19709,7 +19709,9 @@ int PemToDer(const unsigned char* buff, long longSz, int type,
word32 algId = 0;
word32 idx;
#if defined(WOLFSSL_ENCRYPTED_KEYS)
#if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && \
#if ((defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3)) || \
(!defined(NO_AES) && defined(HAVE_AES_CBC) && \
defined(HAVE_AES_DECRYPT))) && \
!defined(NO_WOLFSSL_SKIP_TRAILING_PAD)
int padVal = 0;
#endif
Expand Down Expand Up @@ -20051,6 +20053,17 @@ int PemToDer(const unsigned char* buff, long longSz, int type,
}
}
#endif /* !NO_DES3 */
#if !defined(NO_AES) && defined(HAVE_AES_CBC) && \
defined(HAVE_AES_DECRYPT)
if (info->cipherType == WC_CIPHER_AES_CBC) {
if (der->length > AES_BLOCK_SIZE) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if der->length is a multiple of AES_BLOCK_SIZE? Will there still be padding? If not this logic isn't right. Perhaps the check should be if ((der->length % AES_BLOCK_SIZE) != 0) {?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check is to see whether there is enough bytes for at least one block - a padded block. Must have a full block of padding if the plaintext is a multiple of the block size - otherwise you don't know whether there is padding there or not.
The decryption will fail if the encrypted data isn't a multiple of the block size.

padVal = der->buffer[der->length-1];
if (padVal <= AES_BLOCK_SIZE) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then use if (padVal <= der->length)?

Copy link
Contributor Author

@SparkiDev SparkiDev Mar 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The padding value cannot be more than the block size if using PKCS #5/#7 padding.

der->length -= padVal;
}
}
}
#endif
#endif /* !NO_WOLFSSL_SKIP_TRAILING_PAD */
}
}
Expand Down