Commit Graph

6521 Commits

Author SHA1 Message Date
Gilles Peskine
84a21d5a54 Fix undefined behavior in unsigned-to-signed conversion
The code assumed that `int x = - (unsigned) u` with 0 <= u < INT_MAX
sets `x` to the negative of u, but actually this calculates
(UINT_MAX - u) and then converts this value to int, which overflows.
Cast to int before applying the unary minus operator to guarantee the
desired behavior.
2018-10-12 19:19:12 +02:00
Gilles Peskine
66a28e991d Fix likely-harmless undefined behavior surrounding volatile
The code was making two unsequenced reads from volatile locations.
This is undefined behavior. It was probably harmless because we didn't
care in what order the reads happened and the reads were from ordinary
memory, but UB is UB and IAR8 complained.
2018-10-12 19:15:34 +02:00
Gilles Peskine
d3f978bd95 Add tests for PKCS#1 v1.5 decoding
Functional tests for various payload sizes and output buffer sizes.

When the padding is bad or the plaintext is too large for the output
buffer, verify that function writes some outputs. This doesn't
validate that the implementation is time-constant, but it at least
validates that it doesn't just return early without outputting anything.
2018-10-08 11:49:15 +02:00
Gilles Peskine
23d7ceaca9 PKCS#1 v1.5 decoding: fix empty payload case 2018-10-08 11:49:15 +02:00
Gilles Peskine
5d2391e9aa Indicate the memory access variations in the changelog entry 2018-10-08 11:49:15 +02:00
Gilles Peskine
0b330f764f Remove a remaining sensitive memory access in PKCS#1 v1.5 decryption 2018-10-08 11:49:15 +02:00
Gilles Peskine
03fb3e36e4 mbedtls_rsa_rsaes_pkcs1_v15_decrypt: remove the variable p
Get rid of the variable p. This makes it more apparent where the code
accesses the buffer at an offset whose value is sensitive.

No intended behavior change in this commit.
2018-10-08 11:49:15 +02:00
Gilles Peskine
087544bc98 Minor optimization in the PKCS#1v1.5 unpadding step
Rather than doing the quadratic-time constant-memory-trace on the
whole working buffer, do it on the section of the buffer where the
data to copy has to lie, which can be significantly smaller if the
output buffer is significantly smaller than the working buffer, e.g.
for TLS RSA ciphersuites (48 bytes vs MBEDTLS_MPI_MAX_SIZE).
2018-10-08 11:49:15 +02:00
Gilles Peskine
cf1253e8f0 Use branch-free size comparison for the padding size
In mbedtls_rsa_rsaes_pkcs1_v15_decrypt, use size_greater_than (which
is based on bitwise operations) instead of the < operator to compare
sizes when the values being compared must not leak. Some compilers
compile < to a branch at least under some circumstances (observed with
gcc 5.4 for arm-gnueabi -O9 on a toy program).
2018-10-08 11:49:15 +02:00
Gilles Peskine
a04f8bbd0d Bleichenbacher fix: don't leak the plaintext length (step 2)
Replace memmove(to, to + offset, length) by a functionally equivalent
function that strives to make the same memory access patterns
regardless of the value of length. This fixes an information leak
through timing (especially timing of memory accesses via cache probes)
that leads to a Bleichenbacher-style attack on PKCS#1 v1.5 decryption
using the plaintext length as the observable.
2018-10-08 11:49:15 +02:00
Gilles Peskine
f9dd29e3a8 Bleichenbacher fix: don't leak the plaintext length (step 1)
mbedtls_rsa_rsaes_pkcs1_v15_decrypt takes care not to reveal whether
the padding is valid or not, even through timing or memory access
patterns. This is a defense against an attack published by
Bleichenbacher. The attacker can also obtain the same information by
observing the length of the plaintext. The current implementation
leaks the length of the plaintext through timing and memory access
patterns.

This commit is a first step towards fixing this leak. It reduces the
leak to a single memmove call inside the working buffer.
2018-10-08 11:49:15 +02:00
Gilles Peskine
b473916dcf Evolve choose_int_from_mask to if_int
Make the function more robust by taking an arbitrary zero/nonzero
argument instead of insisting on zero/all-bits-one. Update and fix its
documentation.
2018-10-08 11:49:15 +02:00
Gilles Peskine
ae97c25a91 Add ChangeLog entry 2018-10-08 11:49:15 +02:00
Gilles Peskine
dabe87cd71 Fix a timing-based Bleichenbacher attack on PKCS#1v1.5 decryption
mbedtls_rsa_rsaes_pkcs1_v15_decrypt took care of calculating the
padding length without leaking the amount of padding or the validity
of the padding. However it then skipped the copying of the data if the
padding was invalid, which could allow an adversary to find out
whether the padding was valid through precise timing measurements,
especially if for a local attacker who could observe memory access via
cache timings.

Avoid this leak by always copying from the decryption buffer to the
output buffer, even when the padding is invalid. With invalid padding,
copy the same amount of data as what is expected on valid padding: the
minimum valid padding size if this fits in the output buffer,
otherwise the output buffer size. To avoid leaking payload data from
an unsuccessful decryption, zero the decryption buffer before copying
if the padding was invalid.
2018-10-08 11:49:15 +02:00
Gilles Peskine
cd500f3832 Minor readability improvement
Polish the beginning of mbedtls_rsa_rsaes_pkcs1_v15_decrypt a little,
to prepare for some behavior changes.
2018-10-08 11:49:15 +02:00
Simon Butcher
7a539a57d7 Merge remote-tracking branch 'public/pr/1812' into mbedtls-2.7 2018-09-27 17:19:29 +01:00
Simon Butcher
f73b437fc6 Add ChangeLog entry for PR #1812 2018-09-26 22:59:42 +01:00
Simon Butcher
fa5054ffab Fix ChangeLog entry for PR #1972 2018-09-26 22:59:31 +01:00
Andrzej Kurek
c895821766 ssl-opt.sh: change expected output for large srv packet test with SSLv3
This test also exercises a protection against BEAST
and should expect message splitting.
2018-09-26 22:53:13 +01:00
Andrzej Kurek
3d5cd4ec0a Remove trailing whitespace 2018-09-26 22:53:13 +01:00
Andrzej Kurek
216b40de9b ssl_server2: add buffer overhead for a termination character
Switch to mbedtls style of memory allocation
2018-09-26 22:53:12 +01:00
Andrzej Kurek
d731a6348a Add missing large and small packet tests for ssl_server2 2018-09-26 22:53:12 +01:00
Andrzej Kurek
557335e56d Added buffer_size and response_size options for ssl-server2.
Added appropriate tests.
2018-09-26 22:53:12 +01:00
Simon Butcher
651902d062 Merge remote-tracking branch 'public/pr/1972' into mbedtls-2.7 2018-09-26 22:35:51 +01:00
Simon Butcher
9863afc5e2 Merge remote-tracking branch 'public/pr/1899' into mbedtls-2.7 2018-09-26 22:00:02 +01:00
Simon Butcher
d6a63f4ca5 Clarified ChangeLog entry
ChangeLog entry for backport of #1890 was misleading, so corrected it.
2018-09-13 11:59:03 +01:00
Simon Butcher
eee169a082
Merge pull request #506 from sbutcher-arm/mbedtls-version-2.7.6
Update library version number to 2.7.6
2018-08-31 17:29:24 +01:00
Simon Butcher
34997fd291 Update library version number to 2.7.6 2018-08-31 16:07:23 +01:00
Simon Butcher
a36fe37429 Revised and clarified ChangeLog
Minor changes to fix language, merge mistakes and incorrect classifications of
changes.
2018-08-31 12:00:58 +01:00
Andrzej Kurek
77798b9f2d ssl-opt.sh: change expected output for large srv packet test with SSLv3
This test also exercises a protection against BEAST
and should expect message splitting.
2018-08-29 07:05:59 -04:00
Andrzej Kurek
0d50aeb4d4 Remove trailing whitespace 2018-08-29 07:05:44 -04:00
Andrzej Kurek
fb764931ee ssl_server2: add buffer overhead for a termination character
Switch to mbedtls style of memory allocation
2018-08-29 07:05:44 -04:00
Andrzej Kurek
d5ab1883b7 Add missing large and small packet tests for ssl_server2 2018-08-29 07:05:44 -04:00
Andrzej Kurek
6b3ec17463 Added buffer_size and response_size options for ssl-server2.
Added appropriate tests.
2018-08-29 07:05:44 -04:00
Simon Butcher
9877efb401 Merge remote-tracking branch 'restricted/pr/437' into mbedtls-2.7-restricted 2018-08-28 15:34:28 +01:00
Simon Butcher
242169bdc3 Merge remote-tracking branch 'restricted/pr/498' into mbedtls-2.7-restricted 2018-08-28 15:29:55 +01:00
Simon Butcher
6910201cd1 Merge remote-tracking branch 'restricted/pr/493' into mbedtls-2.7-restricted 2018-08-28 15:23:39 +01:00
Simon Butcher
fbd0ccc0f0 Merge remote-tracking branch 'public/pr/1978' into mbedtls-2.7 2018-08-28 12:32:21 +01:00
Simon Butcher
4102b3d377 Merge remote-tracking branch 'public/pr/1888' into mbedtls-2.7 2018-08-28 12:25:12 +01:00
Simon Butcher
cc4f58d08c Merge remote-tracking branch 'public/pr/1956' into mbedtls-2.7 2018-08-28 12:16:11 +01:00
Simon Butcher
f7be6b029e Merge remote-tracking branch 'public/pr/1960' into mbedtls-2.7 2018-08-28 11:51:56 +01:00
Simon Butcher
040cff9895 Merge remote-tracking branch 'public/pr/1974' into mbedtls-2.7 2018-08-28 10:20:37 +01:00
Simon Butcher
b6cf145b7a Merge remote-tracking branch 'public/pr/1981' into mbedtls-2.7 2018-08-28 10:18:32 +01:00
Hanno Becker
063c50df8a pk_encrypt: Uniformize debugging output 2018-08-23 15:57:15 +01:00
Hanno Becker
45d006a9cc Fix typos in programs/x509/cert_write.c
Fixes #1922.
2018-08-23 15:27:21 +01:00
Hanno Becker
6953ac2dbe Minor formatting improvements in pk_encrypt and pk_decrypt examples 2018-08-23 15:14:52 +01:00
Hanno Becker
20b5d14b28 Adapt ChangeLog 2018-08-23 15:14:51 +01:00
Hanno Becker
a82a6e126d Correct memory leak in pk_decrypt example program 2018-08-23 15:14:50 +01:00
Hanno Becker
20fce25f28 Correct memory-leak in pk_encrypt example program 2018-08-23 15:14:50 +01:00
Hanno Becker
dc6c0e49ad ssl-opt.sh: Preserve proxy log, too, if --preserve-logs is specified 2018-08-22 15:24:25 +01:00