12863 Commits

Author SHA1 Message Date
Valerio Setti
e19a4cfd80 tests: add prototype and implementation of mbedtls_test_ssl_check_context_after_session_reset
Ideally the implementation shouldn't be checked in git, but for the time
being there is no support in building source code for tests from the
binary folder, so it's accepted as a temporary solution.

Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-06-24 14:42:10 +02:00
Valerio Setti
0ff7a2e8b7 tests: scripts: add generate_ssl_session_reset_check.py
Togethere with the framework counterpart ('ssl_session_reset_check.py')
this script is used to automatically generate the function
'mbedtls_test_ssl_check_context_after_session_reset()'.
The goal of this function is to verify that 'mbedtls_ssl_session_reset()'
properly handled (reset or keep) all the fields of the
'mbedtls_ssl_context' structure.

Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-06-24 14:42:10 +02:00
Valerio Setti
150f1c7b40 tests: ssl: split ssl_helpers between public and internal functions
Move functions in "ssl_helpers.h" that depend on internal headers to
"ssl_helpers_internal.h". This allows to include "ssl_helpers.h" also
in programs (which do not have access to internal headers).

Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-06-24 14:42:10 +02:00
Gilles Peskine
657760964e
Merge pull request #1660 from bjwtaylor/info-leak-in-ssl_write_new_session_ticket-4.1
Info leak in ssl write new session ticket 4.1
2026-06-24 09:42:16 +02:00
Gilles Peskine
7674b49934
Merge pull request #1656 from valeriosetti/issue1598-backport4.1
[4.1] PKCS7 accepts weak hashes
2026-06-23 16:40:44 +02:00
Ronald Cron
788a536868
Merge pull request #1673 from bjwtaylor/pkcs-free-stale-pointers-4.1
Pkcs free stale pointers 4.1
2026-06-22 17:54:49 +02:00
minosgalanakis
0e69eb6990
Merge pull request #1667 from gilles-peskine-arm/badmac_seen_or_in_hsfraglen-test-4.1
Backport 4.1: test badmac_seen with renegotiation
2026-06-22 15:49:58 +01:00
Valerio Setti
0b556f5a2d pkcs7: add MBEDTLS_PKCS7_ALLOW_WEAK_SIGNATURES build symbol
It allows weak hash algorithms to be used in PKCS7. It's only added in the
LTS branch for backward compatibility, but it's disabled by default because
that's the safest choice.

This commit also updates test data in order to test the new build symbol.

Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-06-22 11:46:29 +02:00
Valerio Setti
269312540c tests: pkcs7: adjust test dependencies
Newly added tests for RIPEMD160 and MD5 still require SHA-256 for the
certificate being used. SHA-1 is instead useless in this case.

Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-06-22 11:45:56 +02:00
Valerio Setti
154e70b444 test_suite_pkcs7: adjust numbering of tests
Some newly added tests had duplicate numbers with what was already there.
This commit fixes the duplication problem.

Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-06-22 11:45:56 +02:00
Valerio Setti
7cc706ca5a pkcs7: replace MBEDTLS_ERR_PKCS7_VERIFY_FAIL with MBEDTLS_ERR_PKCS7_VERIFY_FAIL
This also updates test data.

Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-06-22 11:45:56 +02:00
Valerio Setti
9253f375c2 tests: pkcs7: add negative tests using MD5 and RIPEMD160
Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-06-22 11:45:56 +02:00
Valerio Setti
de9ee5d6ee tests: pkcs7: adjust return code in SHA-1 based PKCS7 test
Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-06-22 11:45:56 +02:00
Gilles Peskine
2660eea3f0
Merge pull request #1629 from gilles-peskine-arm/basic-constaints-sequence-check-4.1
Backport 4.1: Fix CA bit forgery through invalid basicConstraints
2026-06-18 15:36:46 +02:00
Ben Taylor
c20cd06cb4 Add in the two additional checks that were dropped.
Co-authored-by: Ronald Cron <ronald.cron@arm.com>
Signed-off-by: Ben Taylor <32939606+bjwtaylor@users.noreply.github.com>
2026-06-18 13:04:13 +01:00
Ben Taylor
37e0f109ac Remove pkcs7_parse_free_resets_context as it duplicates pkcs7_parse_reuse
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-06-18 13:04:13 +01:00
Ben Taylor
7448a04683 Add further testing
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-06-18 13:04:13 +01:00
Ben Taylor
fbaf177f2d Fix style issues
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-06-18 13:04:13 +01:00
Ben Taylor
55536a07c9 Remove redundant function from pkcs7_parse_reuse
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-06-18 13:04:13 +01:00
Ben Taylor
b1a4b07509 Add fix for stale pointer after mbedtls_pkcs7_free
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-06-18 13:04:13 +01:00
Gilles Peskine
1f5b89077c
Merge pull request #1655 from bjwtaylor/ECDHE-PSK-integer-overflow-restricted-4.1
Backport Ecdhe psk integer overflow restricted fix to  4.1
2026-06-16 11:49:57 +02:00
Gilles Peskine
dc258c9b9b Remove some test cases that are not really meaningful
Remove the "srv" variant of "DTLS proxy: server: get invalid AD record then
accept renego", where the server initiates the renegotiation process by
sending a HelloRequest. The HelloRequest message is not relevant to this
test. The first message of the actual renegotiation is the ClientHello
message from the client, and the interesting part of the test happens when
the client receives the server's response.

Remove "DTLS proxy: server: get invalid AD record then accept early
renego (cli)" and the corresponding "reject" control. This test case doesn't
really do what it is advertised to do, and in particular does not lead to a
call to `ssl_consume_current_message()` in the server. What happens is that
the client sends a ClientHello message to start the renegotiation, then it
blocks until the server has replied. So there is nothing to delay that
ClientHello after:(unlike what happens in the corresponding client-side
test, where the first flight of the renegotiation protocol contains multiple
messages, so we can delay the first of these messages after the second one).
What happens in practice is that the client times out, then resends its
ClientHello, and the proxy sends the delayed ClientHello immediately after
the resent one, which does not cause any interesting behavior in the server.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-15 19:19:33 +02:00
Gilles Peskine
046e5a43ad Explain the "get invalid AD record .*renego" tests better
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-15 19:19:15 +02:00
Gilles Peskine
cbc94fbdf4 Don't pass badmac_limit when it would have no effect
Only set `badmac_limit` on the side that actually receives a message with a
bad MAC/tag (injected by the proxy).

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-15 19:19:11 +02:00
Gilles Peskine
3d5e440b41 Add DTLS handshake test with badmac_seen=1
Add a test entry point for a handshake with a specially crafted context.
This can be used for defense in depths for scenarios that we think can't
happen, but might actually happen due to a bug, or due to a new feature that
is insufficiently tested. This can also be a proxy for testing aspects of
renegotiation or session resumption in circumstances that are hard to
arrange.

In this commit, use it to test a DTLS handshake with `ssl->badmac_seen`
:(actually `ssl->badmac_seen_or_in_hsfraglen` in the 3.6 branch) set to a
nonzero value. This can't happen, as far as I know (`badmac_seen` can only
be nonzero during renegotiation, not during a nominal handshake or in
session resumption). But it could happen, for example, if
`mbedtls_ssl_session_reset()` failed to reset `badmac_seen`, as was the case
in Mbed TLS 4.0.0 to 4.1.0.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-15 19:18:47 +02:00
Gilles Peskine
cde26bd4ce Add tests for renegotiation with badmac_seen=1 on the server
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-15 19:09:24 +02:00
Gilles Peskine
a21f97139f Add tests for renegotiation with badmac_seen=1 on the client
After a message with a bad MAC (invalid application data record), try to
renegotiate:

* Test cases where renegotiation (nominal or delayed) is rejected.
  Nothing bad happens.
* A test where renegotiation is misparsed and rejected.
  The subsequent parsing fails.
* A test where the renegotiation request is delayed.
  This causes a heap buffer overflow in `ssl_buffer_message()`.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-15 19:08:08 +02:00
Gilles Peskine
b7718ad291 Check "refusing renegotiation" log in more renegotiation tests
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-15 19:07:37 +02:00
Gilles Peskine
84f66de73f Test DTLS renegotiation when badmac_seen_or_in_hsfraglen != 0
Non-regression test for a bug introduced with TLS handshake defragmentation
support in Mbed TLS 3.6.3, whereby the confusion between the `badmac_seen`
meaning and the `in_hsfraglen` meaning of `ssl->badmac_seen_or_in_hsfraglen`
causes renegotiation to be processed incorrectly after receiving a message
with a bad MAC.

The bug doesn't affect Mbed TLS 4.0.0 and above, but it's still good to have
more testing.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-15 19:06:37 +02:00
Gilles Peskine
025422b2e9 Add some hooks in mbedtls_test_ssl_perform_handshake()
Let test code customize the kitchen sink function
`mbedtls_test_ssl_perform_handshake()` at a few points.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-15 19:02:01 +02:00
Gilles Peskine
9fe8059464 Add a missing dependency
Needed by a test certificate.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-15 18:52:26 +02:00
Ronald Cron
91d7da23a8 Remove unnecessary dependency
Signed-off-by: Ronald Cron <ronald.cron@arm.com>
2026-06-15 18:49:12 +02:00
Minos Galanakis
e1896dbfd8 Merge remote-tracking branch 'public/mbedtls-4.1' into public-sync/mbedtls-4.1-14062026
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
2026-06-14 19:36:52 +01:00
Ben Taylor
f2a1306c86 Correct syntax of MBEDTLS_GET_UINT32_BE in write_new_session_ticket_mem
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-06-11 16:24:04 +01:00
Ben Taylor
5fa28bb6cd Improve test of non-initilisation of lifetime
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-06-11 16:24:04 +01:00
Ben Taylor
75cfc5c0b9 Add test for leak
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-06-11 16:24:04 +01:00
Ben Taylor
4afd1c1685 Add in tests for ECDHE-PSK integer overflow
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
(cherry picked from commit c567299ee6bade9547a97d3ad1dcd5f3bd98ea6f)
2026-06-10 14:34:10 +01:00
Gilles Peskine
5427ccf723 Update dependencies in forward port from 3.6
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 11:34:32 +02:00
Gilles Peskine
4e5d350871 Switch to MBEDTLS_ERROR_ADD for combining X509 and ASN1 error codes
Since Mbed TLS 4.0, this is just the low-level error code. But keep the
combination for uniformity with the rest of the file, and to reduce the
differences with the 3.6 branch.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 11:34:32 +02:00
Gilles Peskine
4ac13cc65f Stop accepting basicConstraints with only pathLenConstraint
In an X.509 certificate, a basicConstraints extension where the cA
field (BOOLEAN) is omitted but the pathLenConstraint field (INTEGER )is
present is syntactically valid, but RFC 5280 states that CAs MUST NOT emit
such a certificate.

Historically, since XySSL 0.9 in 2008, if a basicConstraints extension
started with an INTEGER field, we interpreted that field as a cA value. This
was a deliberate change:

> Fixed x509_get_ext() to accept some rare certificates (like
> www.openssl.org:443) which have an INTEGER instead of a BOOLEAN for
> Extension::BasicConstraints::cA.

However, 18 years later, this does not seem to be relevant, and the details
seem to have been lost. Furthermore, major implementations follow RFC 5280
and interpret `SEQUENCE { INTEGER n }` with cA being set to its default
value (FALSE) and pathLenConstraint set to `n`. This means that such a
certificate with n != 0 would be interpreted as a CA certificate in Mbed TLS
but as a leaf certificate elsewhere, which is dangerous.

Since CAs are not supposed to emit such certificates, stop accepting them
altogether.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 11:34:32 +02:00
Gilles Peskine
f6afd20e71 basicConstraints pathLenConstraint tests: use standard cA type
In test cases for pathLenConstraint parsing in a basicConstraints extension
of an X.509 certificate, use the standard type for the leading cA field,
namely BOOLEAN. The test data was encoding that field as INTEGER, which was
accepted as a deviation from the standard since XySSL 0.9.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 11:34:32 +02:00
Gilles Peskine
44b833324b Tighten CSR extensionRequest bounds
Don't allow parts of a compound (SEQUENCE or SET) to go beyond the
containing compound.

The inline CSR regression test is derived from the existing "X509 CSR
ASN.1 (OK)" DER test vector. In the extensionRequest attribute, the original
well-formed fragment is:

    a029 3027 06092a864886f70d01090e 311a 3018 ...

where 31 1a is the SET containing the Extensions sequence and 30 18 is the
contained Extensions sequence. The malformed test changes only the SET
length byte, from 1a to 19:

    a029 3027 06092a864886f70d01090e 3119 3018 ...

The attribute SEQUENCE length and the inner Extensions sequence length are
left unchanged. This makes the SET one byte too short for its containing
attribute, so the parser must reject it with
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH instead of parsing the trailing byte as
data outside the SET.

There is no known security impact, just some risk reduction.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 11:34:13 +02:00
Gilles Peskine
9007fc4b9b Fix lax basicConstraints parsing
When parsing the basicConstraints extension, reject junk after the SEQUENCE
inside the extension (which is probably benign), and reject a SEQUENCE that
extends beyond the extension (could be very dangerous).

Add a non-regression test where a certificate that is technically malformed,
but accepted as a leaf certificate by OpenSSL and other X.509
implementations, to be accepted as a CA certificate by Mbed TLS.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-05-26 20:51:52 +02:00
Minos Galanakis
2e2087d246 test_suite_ssl: Added MBEDTLS_DEBUG_C guards to logs
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
2026-05-20 15:01:43 +01:00
Minos Galanakis
c4461967af test_suite_ssl: Restructured reject_hrr_selecting_unoffered_group
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
2026-05-20 15:01:21 +01:00
Minos Galanakis
d3931632fa test_suite_ssl: Renamed hrr_reject_selecting_unoffered_group
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
2026-05-20 15:01:00 +01:00
minosgalanakis
e862725edd
Merge pull request #10709 from yiwu0b11/remove_unused_ffdh_code-4.1
Backport 4.1: tests: remove remaining FFDH code in compat.sh
2026-05-13 16:49:07 +00:00
Minos Galanakis
be84be5d9a test_suite_ssl: doc fixes
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
2026-05-13 11:45:44 +01:00
Minos Galanakis
2c164902ea Adjusted dependecies for hrr_reject_unadvertised_group
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
2026-05-12 15:02:05 +01:00
Minos Galanakis
bac133ffcb test_suite_ssl: Introduced hrr_reject_unadvertised_group
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
2026-05-12 15:02:05 +01:00