Skip to content

Add Mbed TLS pk key writing, DER and PEM - #84

Merged
Frauschi merged 2 commits into
wolfSSL:masterfrom
padelsbach:pk-write
Dec 2, 2026
Merged

Frauschi merged 2 commits into
wolfSSL:masterfrom
padelsbach:pk-write

Conversation

@padelsbach

@padelsbach padelsbach commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Adds mbedtls/pk.h key writing, with MBEDTLS_PK_WRITE_C:

  • mbedtls_pk_write_key_der and mbedtls_pk_write_pubkey_der. Private keys are RSA or EC, written as PKCS11 or SEC1, also when parsed from PKCS8. Public keys are RSA, EC, X25519 or X448, written as SubjectPublicKeyInfo from a public or private key or a certificate's pk
  • mbedtls_pk_write_key_pem and mbedtls_pk_write_pubkey_pem, with MBEDTLS_BASE64_C
  • mbedtls_pk_write_pubkey, with WOLFPSA_MBEDTLS_3
  • MBEDTLS_ERR_ASN1_BUF_TOO_SMALL, MBEDTLS_MPI_MAX_SIZE and MBEDTLS_ECP_MAX_BYTES, which applications use to size the buffers

Copilot AI balanced review requested due to automatic review settings November 30, 2026 22:41
@padelsbach
padelsbach requested a review from Frauschi November 30, 2026 22:42

Copilot AI 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.

🔵 Needs a closer look

Security-sensitive EC public-point derivation, RNG/blinding requirements, and DER encoding contracts need crypto-maintainer review and validation against the supported wolfCrypt revision, which could not be completed with the available implementation access and backend environment.

No open issues 🎉

What changed in this PR

Adds key writing to wolfPSA’s Mbed TLS compatibility layer, complementing existing key parsing with DER and PEM export.

Changes:

  • Adds RSA and EC private-key export and RSA, EC, X25519, and X448 public-key export.
  • Exposes feature-gated writing APIs, buffer-sizing constants, and the Mbed TLS 3 public-key writer.
  • Adds round-trip, buffer-boundary, certificate-key, and allocation-failure tests, plus compatibility documentation.
File Description
wolfpsa/​tf-psa-crypto/​build_info.h Clarifies the source of the key-writing feature macro in a comment.
wolfpsa/​mbedtls/​pk.h Declares writing APIs and buffer-sizing constants.
tools/​psa_want_probe.c Detects backend support for key writing.
test/​mbedtls_compat/​pk_test.c Adds export, buffer-size, certificate-key, and error tests.
test/​mbedtls_compat/​mbedtls3_test.c Tests generated-key export, allocation failures, and raw public-key writing.
test/​mbedtls_compat/​Makefile Supplies wolfSSL test-key paths to pk_test.
src/​psa_tf_psa.c Implements DER and PEM key writers using wolfCrypt.
src/​psa_tf_psa_internal.h Declares shared encoding forms and the internal DER writer.
src/​mbedtls/​mbedtls3.c Implements the Mbed TLS 3 public-key writing wrapper.
README.md Lists key writing among supported compatibility features.
docs/​mbedtls-compatibility.md Documents APIs, feature requirements, compatibility differences, and tests.

Review effort: Balanced

@Frauschi Frauschi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

6 comments inline, nothing blocking.

Comment thread src/psa_tf_psa.c Resolved
Comment thread src/psa_tf_psa.c Resolved
Comment thread src/psa_tf_psa.c Resolved
Comment thread wolfpsa/mbedtls/pk.h Resolved
Comment thread docs/mbedtls-compatibility.md Resolved
Comment thread src/psa_tf_psa.c Resolved
@Frauschi
Frauschi merged commit e5b35a6 into wolfSSL:master Dec 2, 2026
109 checks passed
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