Skip to content

c509: add CBOR-encoded X.509 (C509) decode/verify module - #98

Open
SparkiDev wants to merge 1 commit into
wolfSSL:mainfrom
SparkiDev:cert_c509
Open

SparkiDev wants to merge 1 commit into
wolfSSL:mainfrom
SparkiDev:cert_c509

Conversation

@SparkiDev

@SparkiDev SparkiDev commented Dec 2, 2026 •

Copy link
Copy Markdown

Add a self-contained C509 (draft-ietf-cose-cbor-encoded-cert) module that decodes and verifies CBOR-encoded certificates, ported into wolfCOSE from the wolfSSL cert_c509 branch and re-skinned to wolfCOSE conventions. It is opt-in (WOLFCOSE_ENABLE_C509, make C509=1) and backed only by general wolfCrypt (ASN.1/RSA/ECC/EdDSA), not by wolfSSL's own C509.

Public API (include/wolfcose/c509.h, wc_CoseC509_*):

  • GetPubKey / GetPubKeyEx: extract the subject public key (Ed25519/Ed448, NIST + brainpool EC incl. compressed-point decompression, RSA modulus), reporting a wc_CoseC509KeyType.
  • Verify / VerifyEx / VerifyEx2: check the issuer signature of a natively signed (type-2) or CBOR-re-encoded X.509 (type-3) certificate, with an optional explicit issuer key type and heap/devId for offload.
  • ToDer: reconstruct the byte-exact DER of a type-3 certificate (compile-time optional via WOLFCOSE_NO_C509_TYPE3).

Design notes:

  • Decodes through wolfCOSE's wolfcose_cbor codec (strict RFC 8949 preferred serialization), replacing the original hand-rolled CBOR parsing.
  • Public boundaries normalize wolfCrypt return codes to WOLFCOSE_E_* via a single mapper; internal code keeps wolfCrypt codes so == comparisons hold. Adds WOLFCOSE_E_MEMORY and WOLFCOSE_E_C509_MALFORMED; LENGTH_ONLY_E is kept as the size-query sentinel.
  • House style: WOLFCOSE_API/LOCAL visibility, no goto, detailed Doxygen (\brief/\param/\return) in the header and brief comments in the .c.
  • Keyed on the linked wolfSSL's hardening for WOLFCOSE_C509_RSA_MIN_BITS (1024 default, 2048 at HARDEN_TLS>=112, 3072 at >=128).

Tests: tests/test_c509.c (21 sub-tests, 69 fixtures) covering type-2/type-3 decode/verify across the supported key families, malformed-input rejection, size queries, and the type-3-disabled build; wired into the test runner.

Build/CI/docs: Makefile C509=1 knob (adds -DWOLFCOSE_ENABLE_C509), a c509.yml workflow building a C509-capable wolfSSL and running the suite under ASan in both type-3-on and WOLFCOSE_NO_C509_TYPE3 arms, and ChangeLog / README / Project-Structure / Macros / API-Reference entries.

Closes #84

@SparkiDev SparkiDev self-assigned this Dec 2, 2026
Copilot AI balanced review requested due to automatic review settings December 2, 2026 06:21

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.

🟡 Changes recommended

C509 heap-allocation paths violate the zero-allocation contract and cause the mandatory check to fail every make test run, even with C509 disabled.

4 open issues
What changed in this PR

Adds opt-in C509 certificate support to wolfCOSE, using its CBOR codec and wolfCrypt for signature verification.

Changes:

  • Adds public-key extraction, signature verification, and type-3 DER reconstruction.
  • Adds build controls, error codes, and test-runner integration.
  • Documents the API and adds AddressSanitizer CI coverage.
File Description
tests/​test_suite.h Declares the C509 test entry point.
tests/​test_main.c Runs C509 tests.
src/​wolfcose_c509.c Implements C509 decoding and verification.
README.md Introduces C509 support and build usage.
Makefile Adds the module and opt-in build flag.
include/​wolfcose/​wolfcose.h Adds memory and malformed-certificate errors.
include/​wolfcose/​c509.h Defines the public API and configuration limits.
docs/​Project-Structure.md Lists the new module and header.
docs/​Macros.md Documents C509 configuration options.
docs/​API-Reference.md Documents APIs and error codes.
ChangeLog.md Records the new feature.
.github/​workflows/​c509.yml Tests with type-3 support enabled and disabled.

Review effort: Balanced

Comment thread src/wolfcose_c509.c Resolved
Comment thread src/wolfcose_c509.c Resolved
Comment thread src/wolfcose_c509.c Resolved
Comment thread src/wolfcose_c509.c Resolved
@SparkiDev
SparkiDev force-pushed the cert_c509 branch 2 times, most recently from 0ba3dbd to e2ef9ee Compare December 2, 2026 11:17
@SparkiDev SparkiDev assigned wolfSSL-Bot and unassigned SparkiDev Dec 2, 2026
@SparkiDev
SparkiDev requested a review from aidangarske December 2, 2026 11:34
Comment thread Makefile Outdated
# pattern. The define is part of the build-config hash below, so toggling C509
# forces the affected objects to rebuild.
ifeq ($(C509),1)
override CFLAGS += -DWOLFCOSE_ENABLE_C509

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.

make C509=1 enables C509 without WOLFCOSE_EXPERIMENTAL since C509 is still a draft can we require both flags in settings.h and add a compile test that fails with WOLFCOSE_ENABLE_C509 alone and passes with both?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

fixed

Comment thread src/wolfcose_c509.c Outdated
if (c509_dec_array(d, &count) != 0) {
ret = ASN_PARSE_E;
}
else if ((count == 0) || ((count % 2) != 0)) {

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.

A device certificate can carry its entire identity in a critical subjectAltName. The draft encodes its empty subject as []. I checked a CA-signed P-256 certificate with that profile: ToDer returns WOLFCOSE_E_UNSUPPORTED. Can we emit the empty DER Name for the subject and add a round-trip test?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

fixed

Comment thread src/wolfcose_c509.c
if (truncated) {
/* A 64-bit epoch beyond word32: reject rather than silently
* saturate it to 0xFFFFFFFF (year 2106). */
ret = ASN_PARSE_E;

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.

The 32-bit limit here also applies to certificate timestamps. A type-3 certificate expiring in 2110 returns WOLFCOSE_E_C509_MALFORMED. Section 3.1.5 allows that date. Can we decode validity as a 64-bit value and keep the 32-bit limit for buffer lengths?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

fixed

-Wall -Wextra -Wpedantic -Wshadow -Wconversion -Wvla \
-Werror=vla -I./include -I$WOLFSSL_DIR/include"
make clean
make C509=1 test CFLAGS="$WOLFCOSE_CFLAGS" \

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.

The ASan job runs the C509 tests, but the line-coverage jobs leave C509 disabled and the coverage script skips missing files. Can we run coverage with C509 enabled and require a result for wolfcose_c509.c so we keep the full coverage?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

fixed

@aidangarske aidangarske 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.

Skoll Multi-Scan Review

Modes: review-security + review

Overall recommendation: REQUEST_CHANGES
Findings: 11 total — 11 posted, 0 skipped
11 finding(s) posted as inline comments (see file-level comments below)

Posted findings

  • [High] [review] C509 bypasses the mandatory experimental-feature gate — include/wolfcose/c509.h:49-55
  • [High] [review] Scratch macros allocate from the heap under SMALL_STACK — src/wolfcose_c509.c:2567-2574
  • [High] [review] SMALL_STACK verification contradicts its public size contract — src/wolfcose_c509.c:4583-4600
  • [High] [review] GetPubKey accepts a non-C509 certificate type — src/wolfcose_c509.c:3461-3472
  • [High] [review-security+review] C509 objects do not depend on their public header — Makefile:201-204
  • [Medium] [review-security+review] Type-3 RSA support is narrower than the documented algorithm matrix — src/wolfcose_c509.c:767-776
  • [Medium] [review] The new MISRA consumer block is never enabled — tests/misra_consumer.c:159-166
  • [Medium] [review] Strict C99 validation never compiles the C509 implementation — .github/workflows/c509.yml:71-76
  • [Medium] [review] Positive tests do not mirror the implementation capability gates — tests/test_c509.c:3496-3511
  • [Medium] [review] Public return contracts omit reachable error codes — include/wolfcose/c509.h:176-177
  • [Low] [review-security] Minimal C509 builds omit the constant-compare helper — src/wolfcose_c509.c:3506-3508

Review generated by Skoll

Comment thread include/wolfcose/c509.h Resolved
Comment thread src/wolfcose_c509.c Resolved
Comment thread src/wolfcose_c509.c Resolved
Comment thread src/wolfcose_c509.c Resolved
Comment thread Makefile Resolved
Comment thread tests/misra_consumer.c Resolved
Comment thread .github/workflows/c509.yml Resolved
Comment thread tests/test_c509.c Resolved
Comment thread include/wolfcose/c509.h Resolved
Comment thread src/wolfcose_c509.c Resolved
@aidangarske aidangarske assigned SparkiDev and unassigned wolfSSL-Bot Dec 2, 2026
@SparkiDev
SparkiDev force-pushed the cert_c509 branch 2 times, most recently from d509f9d to 271c47e Compare December 3, 2026 02:52
Add a self-contained C509 (draft-ietf-cose-cbor-encoded-cert) module that
decodes and verifies CBOR-encoded certificates, ported into wolfCOSE from the
wolfSSL cert_c509 branch and re-skinned to wolfCOSE conventions. It is opt-in
(WOLFCOSE_ENABLE_C509, `make C509=1`) and backed only by general wolfCrypt
(ASN.1/RSA/ECC/EdDSA), not by wolfSSL's own C509.

Public API (include/wolfcose/c509.h, wc_CoseC509_*):
- GetPubKey / GetPubKeyEx: extract the subject public key (Ed25519/Ed448,
  NIST + brainpool EC incl. compressed-point decompression, RSA modulus),
  reporting a wc_CoseC509KeyType.
- Verify / VerifyEx / VerifyEx2: check the issuer signature of a natively
  signed (type-2) or CBOR-re-encoded X.509 (type-3) certificate, with an
  optional explicit issuer key type and heap/devId for offload.
- ToDer: reconstruct the byte-exact DER of a type-3 certificate (compile-time
  optional via WOLFCOSE_NO_C509_TYPE3).

Design notes:
- Decodes through wolfCOSE's wolfcose_cbor codec (strict RFC 8949 preferred
  serialization), replacing the original hand-rolled CBOR parsing.
- Public boundaries normalize wolfCrypt return codes to WOLFCOSE_E_* via a
  single mapper; internal code keeps wolfCrypt codes so == comparisons hold.
  Adds WOLFCOSE_E_MEMORY and WOLFCOSE_E_C509_MALFORMED; LENGTH_ONLY_E is kept
  as the size-query sentinel.
- House style: WOLFCOSE_API/LOCAL visibility, no goto, detailed Doxygen
  (\brief/\param/\return) in the header and brief comments in the .c.
- Keyed on the linked wolfSSL's hardening for WOLFCOSE_C509_RSA_MIN_BITS
  (1024 default, 2048 at HARDEN_TLS>=112, 3072 at >=128).

Tests: tests/test_c509.c (21 sub-tests, 69 fixtures) covering type-2/type-3
decode/verify across the supported key families, malformed-input rejection,
size queries, and the type-3-disabled build; wired into the test runner.

Build/CI/docs: Makefile C509=1 knob (adds -DWOLFCOSE_ENABLE_C509), a c509.yml
workflow building a C509-capable wolfSSL and running the suite under ASan in
both type-3-on and WOLFCOSE_NO_C509_TYPE3 arms, and ChangeLog / README /
Project-Structure / Macros / API-Reference entries.

This branch has not been deployed

No deployments
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.

Feature Request: Add C509 Support for wolfCOSE/wolfSSL

4 participants