Skip to content

Commit 4728469

Browse files
committed
rsa: Re-enable use of zero-length null output
After merging the latest RSA implementation from Mbed TLS, we have a regression in that we no longer properly handle zero-length null output in PKCS1 v1.5 decryption. Prevent undefined behavior by avoiding a memcpy() to zero-length null output buffers.
1 parent 6893364 commit 4728469

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

library/rsa.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,9 +1624,15 @@ int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
16241624
plaintext_max_size,
16251625
plaintext_max_size - plaintext_size );
16261626

1627-
/* Finally copy the decrypted plaintext plus trailing zeros
1628-
* into the output buffer. */
1629-
memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
1627+
/* Finally copy the decrypted plaintext plus trailing zeros into the output
1628+
* buffer. If output_max_len is 0, then output may be an invalid pointer
1629+
* and the result of memcpy() would be undefined; prevent undefined
1630+
* behavior making sure to depend only on output_max_len (the size of the
1631+
* user-provided output buffer), which is independent from plaintext
1632+
* length, validity of padding, success of the decryption, and other
1633+
* secrets. */
1634+
if( output_max_len != 0 )
1635+
memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
16301636

16311637
/* Report the amount of data we copied to the output buffer. In case
16321638
* of errors (bad padding or output too large), the value of *olen

0 commit comments

Comments
 (0)