1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-09-11 12:10:46 +03:00
Commit Graph

32156 Commits

Author SHA1 Message Date
David Horstmann
3f82706cb7 Merge pull request #1349 from felixc-arm/pem-integer-underflow-3.6
[3.6] Fix Integer Underflow when Decoding PEM Keys
2025-06-04 14:36:35 +01:00
Felix Conway
42323eacc9 Add changelog
Signed-off-by: Felix Conway <felix.conway@arm.com>
2025-06-04 10:06:26 +01:00
Felix Conway
6165e71589 Add fix for PEM underflow
Signed-off-by: Felix Conway <felix.conway@arm.com>
2025-06-04 10:06:26 +01:00
Felix Conway
9325883d9f Add test using underflow-causing PEM keyfile
Signed-off-by: Felix Conway <felix.conway@arm.com>
2025-06-04 10:06:17 +01:00
Gilles Peskine
1592639c1f Merge pull request #10182 from gilles-peskine-arm/f_rng-documentation-3.6
3.6 only: document f_rng callbacks
2025-06-03 15:34:23 +00:00
Gilles Peskine
0050f5f394 Update framework with additional operation initialization checks
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-06-03 12:39:32 +02:00
Manuel Pégourié-Gonnard
e51bde06da Fix possible UB in mbedtls_asn1_write_raw_buffer()
This is mostly unrelated to other commits in this PR, except for the
fact that one of the added X.509 tests revealed that with UBSan.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2025-06-03 11:23:19 +02:00
Ronald Cron
381900520f Fix psa_pake_operation_s member types
As the definition of psa_pake_operation_s has
been moved the "xyt_t" structure types can not
be used anymore (defined later).

Signed-off-by: Ronald Cron <ronald.cron@arm.com>
2025-06-03 10:09:00 +02:00
Ronald Cron
d3324fd095 Move PAKE size calculation macros, cipher suite and operation structs
In crypto_extra.h, move PAKE size calculation macros,
the definition of psa_pake_cipher_suite_s and
psa_pake_operation_s just after PAKE type and values
definitions.

This aligns with the order of crypto header inclusions
in crypto.h: crypto_types.h, then crypto_values.h,
then crypto_sizes.h, and then crypto_struct.h.

Take care of keeping them outside of the pake Doxygen
group as they used to be.

Signed-off-by: Ronald Cron <ronald.cron@arm.com>
2025-06-03 10:06:22 +02:00
David Horstmann
23a0d48d1c Merge pull request #10196 from ronald-cron-arm/move-crypto-struct-inclusion
[Backport 3.6] Move the inclusion of crypto_sizes.h and crypto_struct.h in crypto.h
2025-05-28 15:51:09 +00:00
Manuel Pégourié-Gonnard
d9c141749b Merge pull request #1345 from davidhorstmann-arm/pkcs7-side-channel-missing-credit-3.6
Add credit to the reporters of the PKCS7 issue
2025-05-28 11:49:35 +02:00
Ronald Cron
4960825a94 Add change log
Signed-off-by: Ronald Cron <ronald.cron@arm.com>
2025-05-28 10:05:32 +02:00
Ronald Cron
3ea3635d2f Move the inclusion of crypto_sizes.h and crypto_struct.h in crypto.h
That way when API are declared, the types they use are defined.

This should resolve the issues related to psa_xyz_init functions
returning a structure described in #7087.

Signed-off-by: Ronald Cron <ronald.cron@arm.com>
2025-05-28 10:02:12 +02:00
Manuel Pégourié-Gonnard
04fe95d95b Add ChangeLog entry
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2025-05-26 12:38:52 +02:00
Manuel Pégourié-Gonnard
12df5f3a16 Improve unit tests for mbedtls_asn1_store_named_data
Every time we check found->val.p we should also check found->val.len.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2025-05-26 10:55:59 +02:00
Manuel Pégourié-Gonnard
2df7ab7c0c Fix bug in mbedtls_asn1_store_named_data()
When passed a zero-length val, the function was free-ing the buffer as
the documentation suggests:

 * \param val_len   The minimum length of the data buffer needed.
 *                  If this is 0, do not allocate a buffer for the associated
 *                  data.
 *                  If the OID was already present, enlarge, shrink or free
 *                  the existing buffer to fit \p val_len.

However it kept the previous length, leaving the val structure in the
corresponding item in the output list in an inconsistent state:

    p == NULL but len != 0

As a result, functions that would try using this item in the list
(including the same function!) afterwards would trip an dereference the
NULL pointer.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2025-05-26 10:42:14 +02:00
Manuel Pégourié-Gonnard
13f86e689e Add tests for bug in mbedtls_x509_string_to_names()
The commented out tests cause crashes (in different ways) until the bug
is fixed; the first two test are passing already and are here mostly to
provide a reference point.

The bug report was using programs/x509/cert_write, but string_to_names()
is what it was really targetting, which is better for automated tests.
The strings used are a minor adapation of those from the report.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2025-05-22 13:18:49 +02:00
Gilles Peskine
077f8e6353 Restore standard initializers in _init tests
Partially undo "Use short initializers for multipart operation structures",
only in test functions that specifically aim to test initializers. In these
functions, do try with the short initializers, but alongside the standard
ones.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-05-22 10:26:06 +02:00
Gilles Peskine
93dd99571b Use short initializers for multipart operation structures
When initializing a multipart or interruptible operation structure, use an
auxiliary function that doesn't initialize union members to all-bits-zero.
Context: on most compilers, initializing a union to `{0}` initializes it to
all bits zero; but on some compilers, the trailing part of members other
than the first is left uninitialized. This way, we can run the tests on any
platform and validate that the code would work correctly on platforms where
union initialization is short.

This commit makes a systematic replacement in `test_suite_psa_crypto.function`
and `test_suite_psa_crypto_driver_wrappers.function`, which gives good
enough coverage.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-05-22 10:26:06 +02:00
Manuel Pégourié-Gonnard
1e9267c993 Merge pull request #1340 from mpg/fix-string-to-names-uaf-3.6
[3.6] Fix string to names memory management
2025-05-21 14:48:43 +02:00
Manuel Pégourié-Gonnard
8ac3eb9833 Avoid a useless copy in cert_{req,write}
I'm just trying to have a shorter name to avoid repeating a long
expression. This is a job for a pointer, not copying a struct.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2025-05-21 11:17:39 +02:00
Manuel Pégourié-Gonnard
1173786544 Merge pull request #10185 from rojer/tls_consts_3.6
Mark TLS 1.2 algo suite definitons const
2025-05-20 06:55:12 +00:00
Deomid rojer Ryabkov
fb5e2e5e46 Mark ssl_tls12_preset_suiteb_sig_algs const
Signed-off-by: Deomid rojer Ryabkov <rojer@rojer.me>
2025-05-19 20:48:13 +01:00
Deomid rojer Ryabkov
e3aaf82a77 Mark ssl_tls12_preset_default_sig_algs const
To place in flash and save RAM on targets where this applies.

Signed-off-by: Deomid rojer Ryabkov <rojer@rojer.me>
2025-05-19 20:48:08 +01:00
Manuel Pégourié-Gonnard
dad206d25c Merge pull request #10168 from gilles-peskine-arm/union-initialization-gcc15-basic-fix-3.6
Backport 3.6: Fix insufficient union initialization in contexts
2025-05-19 10:31:47 +00:00
Manuel Pégourié-Gonnard
8429619a92 Fix type in ChangeLog
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2025-05-19 12:29:11 +02:00
Manuel Pégourié-Gonnard
8a6fc08607 Add comment on apparent type mismatch
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2025-05-19 12:28:42 +02:00
Manuel Pégourié-Gonnard
35f2220e37 Remove redundant free loop
This version is incomplete. I failed to noticed it when adding a more
complete version, making the existing one redundant.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2025-05-19 12:21:32 +02:00
Gilles Peskine
addf8fc03e Fix ECDSA documentation: blinding is no longer optional
Since Mbed TLS 3.0, blinding is no longer optional in ECDSA.
`mbedtls_ecdsa_write_signature()` and
`mbedtls_ecdsa_write_signature_restartable()` error out if
`f_rng == NULL`. We forgot to update the function documentation.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-05-16 15:00:09 +02:00
Gilles Peskine
c0b357d8b4 ECDSA is a special flower
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-05-16 14:59:42 +02:00
Gilles Peskine
ed10e2ab87 Note functions that store the RNG callback in a context
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-05-16 14:24:50 +02:00
Gilles Peskine
060c70e3ab Reference mbedtls_f_rng_t in public documentation
In public functions that take `f_rng, p_rng` callbacks, link to the
documentation of the callback which is attached to the type name
`mbedtls_f_rng_t`.

Resolves #5868.

```
grep -l -w 'f_rng)' include | xargs perl -i -pe 's/\Qint (*f_rng)(void *, unsigned char *, size_t)\E/mbedtls_f_rng_t *f_rng/g'
```
and include `platform_util.h` where needed.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-05-16 14:19:38 +02:00
Gilles Peskine
1337d540ad Name and document the type of random generator callbacks
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-05-16 14:12:15 +02:00
David Horstmann
ddbf8d030a Add credit to the reporters of the PKCS7 issue
Signed-off-by: David Horstmann <david.horstmann@arm.com>
2025-05-14 15:45:00 +01:00
Gilles Peskine
ff32e86429 Merge pull request #10170 from yanesca/mbedtls-3.6.3.1-pr
Mbedtls 3.6.3.1 PR
2025-05-13 10:19:56 +00:00
Gilles Peskine
91b29a0bd9 Grammar in comments
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-05-13 11:53:31 +02:00
Janos Follath
c5848a5704 Merge branch 'mbedtls-3.6.3.1-rc' into mbedtls-3.6.3.1-pr
This merge was done by the '-s ours' strategy. The only purpose of this
merge is to bring the commit for the 3.6.3.1 release into the
mbedtls-3.6 history and make the CI pass.

Signed-off-by: Janos Follath <janos.follath@arm.com>
2025-05-09 13:03:56 +01:00
Janos Follath
6fb5120fde Remove .gitmodules
Signed-off-by: Janos Follath <janos.follath@arm.com>
v3.6.3.1
2025-05-08 16:06:32 +01:00
Gilles Peskine
65b548386f Changelog entry for the union initialization fixes
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-05-07 18:51:57 +02:00
Gilles Peskine
f4ebf807e6 Test with GCC 15 with sloppy union initialization
This is a non-regression test for
https://github.com/Mbed-TLS/mbedtls/issues/9814

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-05-07 18:51:57 +02:00
Gilles Peskine
0e4907d4f5 Initialize MAC context in internal functions for one-shot MAC
In functions that bypass the API functions and call an internal MAC setup
function directly, make sure to initialize the driver-specific part of the
context. This is a union, and initializing the union to `{0}` only
guarantees that the first member of the union is initialized, not
necessarily the member used by the driver. Most compilers do initialize the
whole union to all-bits-zero, but some don't. With compilers that don't, the
lack of initialization caused failures of the affected operations. This
affected one-shot MAC operations using the built-in implementation.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-05-07 18:42:52 +02:00
Gilles Peskine
bbec1c1d25 Initialize MAC context in internal functions for KDF
In functions that bypass the API functions and call the MAC driver wrapper
`psa_driver_wrapper_mac_sign_setup()` directly, make
sure to initialize the driver-specific part of the context. This is a union,
and initializing the union to `{0}` only guarantees that the first member of
the union is initialized, not necessarily the member used by the driver.
Most compilers do initialize the whole union to all-bits-zero, but some
don't. With compilers that don't, the lack of initialization caused failures
of the affected operations. This affected several key derivation operations.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-05-07 18:42:33 +02:00
Gilles Peskine
a7a480bb81 Initialize driver context in setup functions
In API functions that set up a multipart or interruptible operation, make
sure to initialize the driver-specific part of the context. This is a union,
and initializing the union to `{0}` only guarantees that the first member of
the union is initialized, not necessarily the member used by the driver.
Most compilers do initialize the whole union to all-bits-zero, but some
don't. With compilers that don't, the lack of initialization caused failures
of built-in MAC, interruptible-sign and interruptible-verify. It could also
cause failures for other operations with third-party drivers: we promise
that drivers' setup entry points receive a zero-initialized operation
structure, but this promise was not kept.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-05-07 18:41:59 +02:00
Gilles Peskine
f0daff971b Merge pull request #10151 from gilles-peskine-arm/union-initialization-gcc15-preliminaries-3.6
Backport 3.6: Test with GCC 15
2025-05-07 14:22:28 +00:00
Manuel Pégourié-Gonnard
219c3368eb Merge pull request #1306 from davidhorstmann-arm/pkcs7-padding-side-channel-fix-3.6
[Backport 3.6] Fix side channel in PKCS7 padding
2025-05-06 09:34:40 +02:00
Manuel Pégourié-Gonnard
f9ac5e7728 Add unit test for new behaviour of string_to_names()
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2025-05-05 18:25:26 +02:00
Manuel Pégourié-Gonnard
0803df29fc Fix memory leak in cert_write & cert_req
That memory leak had been present ever since the san command-line
argument has been added.

Tested that the following invocation is now fully valgrind clean:

programs/x509/cert_write san=DN:C=NL,CN=#0000,CN=foo;DN:CN=#0000,O=foo,OU=bar,C=UK;IP:1.2.3.4;IP:4.3.2.1;URI:http\\://example.org/;URI:foo;DNS:foo.example.org;DNS:bar.example.org

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2025-05-05 17:45:43 +02:00
Manuel Pégourié-Gonnard
4dd52b7cfe Fix runtime error in cert_write & cert_req
The runtime error was introduced two commits ago (while avoiding a
use-after-free). Now the programs run cleanly but still leak memory.

The memory leak is long pre-existing and larger than just DN components
(which are made temporarily slightly worse by this commit) and will be
fixed properly in the next commit.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2025-05-05 17:16:15 +02:00
Manuel Pégourié-Gonnard
acdcb7fcd1 Restore behaviour of mbedtls_x509write_set_foo_name()
The documentation doesn't say you can't call these functions more than
once on the same context, and if you do it shouldn't result in a memory
leak. Historically, the call to mbedtls_asn1_free_named_data_list() in
mbedtls_x509_string_to_names() (that was removed in the previous commit)
was ensuring that. Let's restore it where it makes sense. (These are the
only 3 places calling mbedtls_x509_string_to_names() in the library.)

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2025-05-05 16:49:45 +02:00
Manuel Pégourié-Gonnard
19d2c9165a Fix undocumented free() in x509_string_to_names()
Now programs/x509/cert_write san="DN:CN=#0000;DN:CN=#0000" is no longer
crashing with use-after-free, instead it's now failing cleanly:

 failed
  !  mbedtls_x509_string_to_names returned -0x2800 - X509 - Input invalid

That's better of course but still not great, will be fixed by future
commits.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2025-05-05 16:44:18 +02:00