33174 Commits

Author SHA1 Message Date
Manuel Pégourié-Gonnard
7e51c31aef rsa/psa: tune doc about RSA v1.5 decrypt usage
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-06-08 11:39:44 +02:00
Gilles Peskine
fe47e9ed89 Define and use mbedtls_rsa_decrypt_decompose_ret()
Use a more structured way of isolating and translating sensitive error codes
from RSA PKCS#1v1.5 decryption.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-06-08 11:39:44 +02:00
Gilles Peskine
a818c0a8dd Changelog entry for badmac_seen_or_in_hsfraglen confusion
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 21:59:07 +02:00
Ronald Cron
b513676bed
Merge pull request #10772 from mpg/security-link-3.6
[3.6] Update broken link
2026-06-05 16:47:25 +00:00
Minos Galanakis
c3f90ec54d test_suite_ssl: use 3.6 endpoint helper signatures in HRR test
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
2026-06-05 15:16:19 +01:00
Gilles Peskine
24c2875cb0 Fix confusion between ssl->badmac_seen and ssl->in_hsfraglen
Since Mbed TLS 3.6.3, the field `badmac_seen_or_in_hsfraglen` in the
`mbedtls_ssl_context` structure has a dual purpose: it is the new name of
`badmac_seen` for DTLS, and it has a new meaning `in_hsfraglen` for TLS.
This was done to add support for handshake message fragmentation in TLS
while preserving the library ABI.

There was a bug in some places in the code, which reads the dual-purpose
field in the wrong protocol. Specifically:

* `mbedtls_ssl_prepare_handshake_record()` (beginning): mistakenly reads
  `badmac_seen_or_in_hsfraglen` in DTLS to mean `in_hsfraglen`. (DTLS
  handshake defragmentation does not use `in_hsfraglen`.) As a
  consequence, in DTLS, after receiving a record with a bad MAC, the field
  `ssl->in_hslen` was not correctly updated, which causes problems further
  in handshake message processing.
* `mbedtls_ssl_prepare_handshake_record()` (end): correctly updates
  `badmac_seen_or_in_hsfraglen` in TLS with the `in_hsfraglen` meaning.
* `ssl_consume_current_message()`: incorrectly read
  `badmac_seen_or_in_hsfraglen` in DTLS to mean `in_hsfraglen`, and thus
  stops processing messages after receiving a record with a bad MAC.
* `ssl_get_next_record()`: accesses `badmac_seen_or_in_hsfraglen` to mean
  `badmac_seen` in DTLS. No bug here.
* `mbedtls_ssl_handle_message_type()`: reads `badmac_seen_or_in_hsfraglen`
   to mean `in_hsfraglen` in TLS. No bug here.
* `mbedtls_ssl_session_reset_msg_layer()`: both fields need to be reset
  when not partial, and a partial reset only applies to DTLS where we do
  want to preserve `badmac_seen` in a partial reset. No bug here.
* `mbedtls_ssl_context_save()`, `mbedtls_ssl_context_load()`: saving
  `badmac_seen` in DTLS is desired and done. Saving `in_hsfraglen` in TLS
  is harmless.

To fix this, in `ssl_msg.c`, always access the dual-purpose field
`badmac_seen_or_in_hsfraglen` through getters and setters that are specific
to one purpose and that check that the purpose matches the protocol.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 15:40:28 +02:00
Gilles Peskine
397db25dc3 Add tests demonstrating badmac_seen_or_in_hsfraglen confusion on the server
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 15:40:28 +02:00
Gilles Peskine
305bab2bbc Add renegotiate=2 option to ssl_client2
In `ssl_client2`, the existing option `renegotiate=1` causes the client to
initiate renegotiation immediately after the initial handshake. Add a
variant `renegotiate=2` that initiates renegotiation after one data exchange.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 15:40:28 +02:00
Gilles Peskine
ee9ce31e96 Add tests demonstrating badmac_seen_or_in_hsfraglen confusion 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-05 15:40:28 +02:00
Gilles Peskine
9124556b6d Check "refusing renegotiation" log in more renegotiation tests
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 15:40:28 +02:00
Gilles Peskine
0fb05d3520 Add badmac_limit option to ssl_client2
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 15:40:28 +02:00
Gilles Peskine
a9fa52a025 Add one-shot bad AD options to udp_proxy
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 15:40:28 +02:00
Gilles Peskine
e8d780f5af Add udp_proxy options to delay an encrypted handshake message
This can be used to simulate data messages received in the wrong epoch, or
to cause a handshake message to be buffered.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 15:40:28 +02:00
Gilles Peskine
929ce501bd 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.2, 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.

This bug will be fixed in a subsequent commit.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 15:40:28 +02:00
Gilles Peskine
ba656e8f9b 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-05 15:40:28 +02:00
Gilles Peskine
2aa80688d9 Add a missing dependency
Needed by a test certificate.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 15:40:28 +02:00
Manuel Pégourié-Gonnard
70d619741b Update broken link
I believe the existing link was to the TF wiki which has been retired
for some time now.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-06-05 12:28:55 +02:00
Ronald Cron
d774a25bfe Remove unnecessary dependency
Signed-off-by: Ronald Cron <ronald.cron@arm.com>
2026-06-05 12:03:16 +02:00
Gilles Peskine
1aaec34173 Add CVE-ID
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 11:33:28 +02:00
Gilles Peskine
8c398012dd Add attribution for the second bug
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 11:33:28 +02:00
Gilles Peskine
8e07717105 basicConstraints with leading INTEGER field: document the behavior change
Announce the security fix together with the overflow fix, since the two bugs
are pretty much indistinguishable from a black-box perspective, despite
being due to independent problems in the code.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 11:33:27 +02:00
Gilles Peskine
f67b8e5bde 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:33:27 +02:00
Gilles Peskine
4fb9c9e439 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:33:27 +02:00
Gilles Peskine
80807b3af8 Remove unreachable CSR extensionRequest check
It is now unreachable because:

- end_set == end_attr_data is enforced at library/x509_csr.c:236.
- end_exts == end_set is enforced at library/x509_csr.c:248.
- x509_csr_parse_extensions() only returns success if *p == end_exts, enforced
  at library/x509_csr.c:188.

So after a successful x509_csr_parse_extensions() call, *p == end_exts ==
end_set == end_attr_data. The condition at line 257 cannot be true.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-06-05 11:33:23 +02:00
Gilles Peskine
102d53ea9e 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:33:16 +02:00
Ben Taylor
723cbadb53 Add ChangeLog
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-06-05 10:20:36 +01:00
Ben Taylor
e3be807e5c Add fix for OOB key exchange error
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-06-05 10:18:42 +01:00
Ben Taylor
a6023aef67 update code docs
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-06-03 11:10:36 +01:00
Ben Taylor
2ddd6a2f25 Improve comments and guards
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-06-02 13:50:32 +01:00
Manuel Pégourié-Gonnard
ea0a2c6d03 rsa/psa/pk: warn more against PKCS#1 v1.5 decrypt
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-06-02 11:10:06 +02:00
Manuel Pégourié-Gonnard
54682ec425 rsa: add CF testing for PKCS#1 v1.5 decode
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-06-02 11:10:06 +02:00
minosgalanakis
915442dbed
Merge pull request #1614 from yiwu0b11/mbedtls_pk_ecc_set_pubkey-overread-3.6
Backport 3.6: Fix 1-byte overread in ECC key parsing
2026-06-02 10:05:35 +01:00
Manuel Pégourié-Gonnard
8f4a4eac98 pk: fix side channel in RSA decryption
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-06-02 09:36:41 +02:00
Manuel Pégourié-Gonnard
689a05df0b pk: test RSA decrypt with buffer too small
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-06-02 09:36:38 +02:00
Manuel Pégourié-Gonnard
682d8f3e89 psa-rsa: also protect OUTPUT_TOO_LARGE
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-06-02 09:24:16 +02:00
Ben Taylor
85f3f6f894 Add missing header
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-06-02 08:19:02 +01:00
Ben Taylor
b555410883 Add mbedtls_chacha20_check_counter_wrap to header
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-06-01 14:43:50 +01:00
Manuel Pégourié-Gonnard
f1dc979f27 psa: avoid leaking padding errors
Padding errors should be indistiguishable from success.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-06-01 10:10:14 +02:00
Manuel Pégourié-Gonnard
503c1375c8 psa-rsa: fix side channel on invalid padding
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-06-01 10:08:52 +02:00
Manuel Pégourié-Gonnard
859cf0788d psa-rsa: call pkcs1_v15 function directly
Like the OAEP path calls the OAEP function directly

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-06-01 09:52:33 +02:00
Ronald Cron
4c70413087
Merge pull request #1555 from gilles-peskine-arm/ssl_tls13_prepare_new_session_ticket-psa_get_random-3.6
Backport 3.6: Fix ssl_tls13_prepare_new_session_ticket returning 1 on an RNG failure
2026-05-29 16:19:37 +02:00
Ben Taylor
ea9848e14a Adapt ChaCha20 counter helper for 3.6
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-05-29 13:24:49 +01:00
Ben Taylor
96cb584a5e Reject overlong PSA ChaChaPoly lengths
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-05-29 13:17:53 +01:00
Ben Taylor
3df9fac129 Reject overlong ChaChaPoly updates
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-05-29 13:17:45 +01:00
Ben Taylor
dcf3b11749 Tidy ChaCha20 counter limit handling
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-05-29 13:17:36 +01:00
Ben Taylor
7bf0215f8d Add guard to prevent wraparound if SIZE_MAX > UINT64_MAX in chacha20
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-05-29 13:17:29 +01:00
Ben Taylor
3a358b0677 Remove unused cast
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-05-29 13:17:21 +01:00
Ben Taylor
130b914ff2 remove duplicate chacha20_check_counter_wrap functionality in chacha20 and chacha20_neon
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-05-29 13:17:14 +01:00
Ben Taylor
0e9be79ea3 Add simplification of if statement in chacha20
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-05-29 13:17:07 +01:00
Ben Taylor
56b050d738 Add use of keystream_bytes_used as the exhausted marker
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-05-29 13:17:00 +01:00