Skip to content

Fix SE050 example error handling and default the cert/key client to ECC - #661

Open
LinuxJedi wants to merge 13 commits into
wolfSSL:masterfrom
LinuxJedi:fenrir-se050-examples
Open

LinuxJedi wants to merge 13 commits into
wolfSSL:masterfrom
LinuxJedi:fenrir-se050-examples

Conversation

@LinuxJedi

Copy link
Copy Markdown
Member

Error handling fixes for the SE050 examples from Fenrir, plus the problems
found while testing them on the SE050 simulator and on a real SE051. One
commit per fix, and each commit message has the details.

Fenrir findings:

  • F-11749: the CSR example returned success and skipped cleanup after a
    failure, leaving the generated key in the SE050.
  • F-13358: the key/cert insert example returned success for an invalid ECDSA
    signature, and did not always free the ECC key.
  • F-13359: the key/cert insert example returned success when a certificate
    read back from the SE050 did not match.
  • F-13360: the key/cert insert example overwrote an insert failure with the
    result of a later insert.
  • F-13361: the key/cert insert example entry point always returned success.
  • F-15615: the cert/key client example overwrote an SE050 configuration
    failure and carried on.
  • F-15616: the cert/key client example could close a socket it never opened.
  • F-15617: both TLS client examples returned success after a failed TLS
    handshake.

Found in testing:

  • The CSR example left the signature type at the RSA default while signing
    with an ECC key. Current wolfSSL rejects that (ALGO_ID_E, -133), so no CSR
    was produced.
  • The cert/key client example had the same insert and erase handling as
    F-13360 and F-13361, fixed the same way.
  • The examples insert objects at key IDs 0x5002 to 0x5007, which wolfSSL
    rejects (-173) unless it is built with -DSE050_KEYID_START=0x0000FFFF. The
    top level README configure example did not set it. Documented in the three
    READMEs.
  • The cert/key client example defaulted to RSA, which cannot work with a
    default wolfSSL build: wolfSSL cannot make an RSA-PSS signature with an RSA
    key that is held in the SE050, and RSA-PSS is used for RSA client
    authentication in TLS 1.3 (and in TLS 1.2 when it is compiled in), so
    wolfSSL_connect() fails with -193. The default is now ECC, and the README
    says the RSA option needs --disable-rsapss --disable-tls13.

Related: wolfSSL/wolfssl#12357 fixes wc_se050_get_binary_object() writing
past the caller's length on 64-bit hosts, which the cert/key client and the
key/cert insert example both call.

Testing

SE050 simulator (wolfSSL/simulators SE050Sim, plug-and-trust v04.07.01,
64-bit Linux), original and fixed examples side by side with faults injected:
an invalid signature, a truncated key, a certificate mismatch, a PEM buffer
that is too small, an SE050 configuration failure and a peer that is not a TLS
server. The originals exited 0 in each case, the fixed examples exit 1, and
the unmodified runs still exit 0. valgrind shows the close() on an
uninitialised descriptor before the change and nothing after.

Real hardware (SE051 on an STM32U585, wolfSSL master, example sources
compiled unmodified, with a wolfSSL server on the same board for the TLS
clients):

Example Before After
wolfcrypt_generate_csr sign fails (-133), reports success, key left in the SE051 success, key erased, CSR verifies with OpenSSL
wolfcrypt_key_cert_insert success success, all six objects erased
wolfssl_client success (TLS 1.3) success (TLS 1.3)
wolfssl_client_cert_key, RSA handshake fails (-193), reports success handshake fails (-193), reports failure
wolfssl_client_cert_key, ECC not run success (TLS 1.3), certificate read back from the SE051

The RSA configuration of wolfssl_client_cert_key succeeds on the SE051 and
on the simulator when wolfSSL is built without RSA-PSS and TLS 1.3. The SE051
object count was the same before and after every run of the fixed examples.

ex_sss_entry() returned kStatus_SSS_Success whatever happened, and every
cleanup step was skipped once ret was non-zero. A failure after the key was
generated therefore left the key in the SE050 and the ECC key and RNG
unfreed, with the demo still exiting 0.

Track what was set up, always erase the generated key and free the ECC key
and RNG, keep the first error rather than the last cleanup result, and
return kStatus_SSS_Fail when anything failed.
wc_InitCert() defaults sigType to an RSA signature, but the example signs
with an ECC key. Current wolfSSL rejects a signature type that does not
match the key, so wc_SignCert_ex() failed with ALGO_ID_E (-133) and no CSR
was produced. Set CTC_SHA256wECDSA.
se050_verify_ecc_sig_rs() returned 0 when wc_ecc_verify_hash() ran but
reported the signature as invalid, and only freed the ECC key when nothing
had failed. Return SIG_VERIFY_E for an invalid signature, and free the key
whenever it was initialized without losing an earlier error.
read_certs_from_se050() counted size and content mismatches in err but
returned ret, which still held the successful read result, so a certificate
that came back different was reported as success. Return an error whenever
err is set.
…mple

In write_certs_keys_to_se050() the RSA certificate, ECC private key and ECC
certificate inserts ran unconditionally and assigned to ret, so an earlier
insert failure was overwritten by a later success. Only run each insert if
everything before it succeeded.
ex_sss_entry() logged failures through ret but always returned the success
status it was initialized with. The erase step also overwrote ret, and
erase_certs_keys_from_se050() only returned the result of its last erase.

Return kStatus_SSS_Fail when ret is non-zero, keep an earlier failure when
the samples are erased, and have the erase function try every object and
return the first error.
write_certs_keys_to_se050() was called even when wc_se050_set_config() had
failed, and its result replaced ret, so the example carried on without a
working SE050 configuration. Only write the credentials when the
configuration succeeded.
…mple

sockfd was uninitialized and only assigned when the earlier setup
succeeded, but cleanup always called close(sockfd), which could close an
unrelated descriptor. Initialize it to -1 and only close a socket that was
opened.
Both client examples only treated ret == WOLFSSL_FAILURE as a failure, but
wolfSSL_connect() returns WOLFSSL_FATAL_ERROR, so a failed handshake still
returned kStatus_SSS_Success. In the cert/key client a successful erase of
the credentials also reset ret to WOLFSSL_SUCCESS.

Treat anything other than WOLFSSL_SUCCESS as a failure, and do not let the
credential erase clear an earlier failure.
Same handling as the key/cert insert example: do not insert the
certificate after the private key insert failed, and have
erase_certs_keys_from_se050() return the first erase failure instead of
the result of the last erase.
wolfSSL only allows keys and certificates to be inserted at key IDs below
SE050_KEYID_START, which defaults to 100. The examples use 0x5002 to
0x5007, so with a default wolfSSL build the inserts fail with BAD_FUNC_ARG
(-173). Add the define to the configure example and explain it in the
READMEs of the two examples that insert objects.
wolfSSL cannot make an RSA-PSS signature with an RSA private key that is
held in the SE050, and RSA-PSS is what TLS 1.3, and TLS 1.2 when it is
compiled in, uses to authenticate an RSA client certificate. With a default
wolfSSL build the RSA configuration of this example therefore failed in
wolfSSL_connect() with BAD_PADDING_E (-193), on a real SE051 as well as on
the simulator.

Make ECC the default, which works with TLS 1.3, and document that the RSA
option needs wolfSSL built with --disable-rsapss --disable-tls13. Update
the README instructions and sample output to match.
Copilot AI balanced review requested due to automatic review settings December 2, 2026 16:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A certificate-insertion failure after successful private-key insertion skips cleanup in both RSA and ECC modes, leaving the private key in the SE050 without rollback.

1 open issue
What changed in this PR

Improves error handling and cleanup across the SE050 examples, and defaults the certificate/key TLS client to ECC.

Changes:

  • Preserve operation failures and report unsuccessful runs correctly.
  • Improve resource cleanup and select ECDSA for CSR signing.
  • Document key-ID configuration, ECC defaults, and RSA restrictions.
File Description
SE050/​wolfssl/​wolfssl_client/​wolfssl_client.c Report TLS handshake failures correctly.
SE050/​wolfssl/​wolfssl_client_cert_key/​wolfssl_client_cert_key.c Default to ECC and improve failure handling and cleanup.
SE050/​wolfssl/​wolfssl_client_cert_key/​README.md Document ECC usage, RSA restrictions, and key-ID requirements.
SE050/​wolfssl/​wolfcrypt_key_cert_insert/​wolfcrypt_key_cert_insert.c Preserve insertion, verification, and cleanup errors.
SE050/​wolfssl/​wolfcrypt_key_cert_insert/​README.md Explain required key-ID configuration.
SE050/​wolfssl/​wolfcrypt_generate_csr/​wolfcrypt_generate_csr.c Select ECDSA signing and improve cleanup and failure reporting.
SE050/​README.md Add and explain the library’s key-ID configuration.

Review effort: Balanced

@LinuxJedi

Copy link
Copy Markdown
Member Author

@claude review

write_certs_keys_to_se050() inserts the private key and then the
certificate. When the certificate insert failed, the example skipped the
erase at the end because nothing was recorded as stored, and the private
key stayed in the SE050.

Record the write as attempted before its result is known, so the erase
runs and removes whatever was inserted.

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #661

Scan targets checked: wolfssl-examples-src, wolfssl-examples-bugs
Not reviewed (no scan target covers them): SE050/README.md, SE050/wolfssl/wolfcrypt_key_cert_insert/README.md, SE050/wolfssl/wolfssl_client_cert_key/README.md
Coverage: 4 of 4 in-scope changed file(s) opened by the reviewer

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Max

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed this PR and didn't find any bugs. It's a 13-commit change to SE050 key handling and cleanup that also switches the cert/key client's default from RSA to ECC, and there's a later bot review I can't see, so a human look is still worthwhile.

What was reviewed:

  • CSR example: the rngInit/eccInit/keyStored flags make cleanup run even after an earlier failure. The first error is kept and turned into kStatus_SSS_Fail. sigType is now CTC_SHA256wECDSA, which matches the ECC key passed to wc_MakeCertReq_ex.
  • Key/cert insert and cert/key client: each insert runs only if the one before it succeeded, the erase functions try every object and return the first error, and a successful erase can no longer hide an earlier failure.
  • Cert/key client: credentialsStored is set before the write, so a partial write still gets erased (this covers the earlier inline thread). sockfd starts at -1 and is closed only if it was opened. Both TLS clients now treat any result other than WOLFSSL_SUCCESS as a failure.
  • Not done: nothing was built or run against the SE050 simulator or hardware. The hardware results in the description come from the author.
Extended reasoning...

The change touches four SE050 example C files and three READMEs, about 270 lines. The C changes fix error propagation and cleanup around keys and certificates stored in the secure element, and the READMEs document the SE050_KEYID_START build requirement. The secure-element key handling is crypto-adjacent, and the security surface is limited to making sure example keys are erased and failures are reported. I read the error flow in each file and found no bugs. I deferred because the change is mid-sized and switches the example's default from RSA to ECC, and because a bot review posted after the last commit can't be checked from here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants