Skip to content

Report a refused client certificate and an untrusted server - #75

Open
Frauschi wants to merge 2 commits into
wolfSSL:mainfrom
Frauschi:tls-reject-errors
Open

Frauschi wants to merge 2 commits into
wolfSSL:mainfrom
Frauschi:tls-reject-errors

Conversation

@Frauschi

@Frauschi Frauschi commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

A refused client certificate or an untrusted server used to look like a network error (WOLFCERT_ERR_IO or a bare WOLFCERT_ERR_TLS), so an application could not tell "fall back to the IDevID" from "retry". Two new codes tell them apart, and a failed TLS setup now names the step that failed.

Changes

  • New WOLFCERT_ERR_TLS_PEER_REJECTED (-17): only for an authenticated certificate alert (TLS 1.3, verified server, finished handshake, client cert loaded).
  • New WOLFCERT_ERR_TLS_UNTRUSTED (-18): local server-certificate verification failed, or a trust anchor is not valid at the device time.
  • Every other received fatal alert is WOLFCERT_ERR_TLS, with the alert named and marked unauthenticated where it is (TLS 1.2, before the handshake, unverified server).
  • wolfSSL stops reading after a send fails with EPIPE and never sees the server's alert; the built-in transport maps EPIPE/ECONNRESET/ENOTCONN on write to CONN_CLOSED so the alert is read. Custom transports keep failing at once.
  • setup_tls_ex failures carry a message and the wolfSSL code; allocation failures return WOLFCERT_ERR_MEMORY.
  • Header contracts, AGENTS.md, ARCHITECTURE.md 4.6 and EMBEDDED.md updated.

Testing

  • test_tls_http: TLS 1.2 and 1.3 refusals, forged plaintext alert and forged TLS 1.2 ServerHello, unverified server, PHA refusal, refused writes, custom transport fail-fast, untrusted anchor/host/expiry/clock.
  • test_est_mtls_roundtrip expects the new codes; test_smoke checks every wolfcert_strerror text.

Merge note

Conflicts with #74 in setup_tls_ex; whichever merges second rebases.

Fixes F-18189, F-18190.

setup_tls_ex returned a bare WOLFCERT_ERR_TLS for a failed
wolfSSL_CTX_new, wolfSSL_CTX_SetDevId, trust-anchor load,
client-certificate load or wolfSSL_new, with no message and no wolfSSL
code. wolfcert_last_error_message() came back empty and the CLI printed
just "TLS error", hiding the two common causes: a device clock that
makes wolfSSL refuse the trust anchor as not yet valid
(ASN_BEFORE_DATE_E), and a malformed client certificate.

Each step now sets a message naming it and keeps wolfSSL's code for
wolfcert_last_wolfssl_err(). A bare WOLFSSL_FAILURE is not recorded as a
wolfSSL code, since it names no cause. An allocation failure reports
WOLFCERT_ERR_MEMORY instead of WOLFCERT_ERR_TLS, including a NULL from
wolfSSL_CTX_new or wolfSSL_new; their messages also name the other
causes of that NULL, a wolfSSL init failure and an RNG failure. Both
client-key loads use the same helper, and a client_key_obj with a key id
in a build without key id support reports WOLFCERT_ERR_UNSUPPORTED, as
keygen does, instead of a TLS error with code 0.

The session-open OOM sweep in test_tls_http now requires
WOLFCERT_ERR_MEMORY for every failed allocation, except under
OPENSSL_EXTRA, where wolfSSL reports some anchor-load allocation
failures as ASN_PARSE_E or BAD_MUTEX_E. New cases cover a garbage client
certificate and a trust anchor loaded with the clock at 1970; wolfSSL
built with --enable-all loads that anchor anyway, so the case checks the
failure only when there is one.

Fixes F-18190.
Copilot AI balanced review requested due to automatic review settings November 20, 2026 12:50
@Frauschi Frauschi self-assigned this Nov 20, 2026

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

Certificate trust failures are incompletely mapped, and the expiry test’s timestamp can overflow signed 32-bit time_t, causing false failures on affected platforms.

2 open issues
What changed in this PR

This PR makes wolfCert’s TLS failures more actionable by distinguishing authenticated client-certificate rejection from server trust failures, helping applications choose between identity fallback and retry.

Changes:

  • Adds two TLS error codes and more detailed setup and alert diagnostics.
  • Preserves pending TLS alerts after built-in transport write failures.
  • Expands regression tests and documents the updated error contracts.
File Description
wolfcert/​types.h Clarifies closed-peer transport write results.
wolfcert/​scep.h Documents HTTPS session error codes.
wolfcert/​http.h Documents TLS failures and session closure behavior.
wolfcert/​est.h Updates EST session error contracts.
wolfcert/​errors.h Defines rejection and trust-failure codes.
tests/​unit/​test_smoke.c Checks error-string coverage.
tests/​unit/​test_net.c Updates closed-peer write expectations.
tests/​integration/​test_tls_http.c Tests TLS classification, refused writes, and setup diagnostics.
tests/​integration/​test_est_mtls_roundtrip.c Asserts specific certificate-rejection results.
src/​net_posix.c Maps closed-peer write errors to connection closure.
src/​http.c Classifies TLS failures and preserves pending alerts.
src/​errors.c Adds descriptions for the new error codes.
docs/​EMBEDDED.md Updates the hostname-verification error reference.
docs/​ARCHITECTURE.md Explains refused writes and session closure.
AGENTS.md Documents TLS failure handling for integrators.

Review effort: Balanced

Comment thread tests/integration/test_tls_http.c Resolved
Comment thread src/http.c Resolved
@Frauschi
Frauschi requested a balanced review from Copilot November 20, 2026 13:11
@Frauschi

Copy link
Copy Markdown
Member Author

@claude review

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

Missing RSA_KEY_SIZE_E and ECC_KEY_SIZE_E cases in src/http.c cause certificate rejections under the minimum-key-size policy to return generic WOLFCERT_ERR_TLS instead of the documented WOLFCERT_ERR_TLS_UNTRUSTED.

1 open issue
2 resolved since last review

Review effort: Balanced

Comment thread src/http.c Resolved

@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 changes how TLS failures are classified, and it changes the built-in transport so that a write to a closed socket counts as sent. A human with wolfSSL experience should still look at both.

What was reviewed:

  • tls_failure: PEER_REJECTED is returned only when the connection is TLS 1.3, the handshake finished, the server was verified and a client cert was loaded. TLS 1.2 and pre-handshake alerts are labelled unauthenticated.
  • wolfcert_cbio_send / posix_write: EPIPE, ECONNRESET and ENOTCONN now count as written (tx_closed) so wolfSSL reads the server's alert. This applies only when the built-in read is used, and sm_done closes the session afterwards so it isn't reused.
  • setup_tls_ex error mapping (allocation failures become WOLFCERT_ERR_MEMORY, trust anchors outside their validity dates become UNTRUSTED) and the matching header and doc updates.
Extended reasoning...

The change touches the TLS client path in src/http.c and src/net_posix.c: alert-based error classification, a list of wolfSSL verification errors that map to a new UNTRUSTED code, and a send callback that reports failed writes as successful so the peer's alert can still be read. This is security-sensitive surface (TLS authentication and trust decisions), and applications are meant to choose between an IDevID fallback and a retry based on these codes. The diff is about 880 added lines, mostly new integration tests. The two Copilot threads were resolved by the author after a follow-up commit, which is not independent confirmation that they were addressed. Given the TLS surface and the size of the change, a human review is warranted instead of an automated approval.

A server that refused the client certificate looked exactly like a
dropped connection: the client got WOLFCERT_ERR_IO, or WOLFCERT_ERR_TLS
with SOCKET_ERROR_E. An application could not tell "the LDevID was
rejected, fall back to the IDevID" from "network problem, retry". The
server does send the alert, and it reaches the client's socket buffer,
but wolfSSL never reads it. The server closes right after the alert, the
client's next send fails with EPIPE, and from then on wolfSSL returns
that send error from every read without looking at the socket. This
happens under TLS 1.2 inside wolfSSL_connect and under TLS 1.3 on the
first write after the handshake, with wolfSSL 5.9.4 and master.

The built-in transport now reports EPIPE, ECONNRESET and ENOTCONN (which
some stacks return for a reset connection) on write as
WOLFCERT_ERR_CONN_CLOSED. When the session reads through the built-in
read, the wolfSSL send callback drops that write but reports it sent,
and drops later writes the same way, so wolfSSL reads on, processes the
alert and fails with FATAL_ERROR. The built-in read returns at once on a
closed or reset socket; a custom read might instead wait for a reply the
server never sends and hang a blocking request, so with any other read a
CONN_CLOSED write fails at once, as before. A peer that closed without
an alert still ends in an I/O or TLS error once the read reaches the
close. When an exchange completes after a dropped write, the session
closes, so the next request gets WOLFCERT_ERR_CONN_CLOSED instead of
having its writes dropped. ARCHITECTURE.md section 4.6 and the transport
contract in types.h now say a write to a closed or reset peer may return
WOLFCERT_ERR_CONN_CLOSED. A reset on read stays WOLFCERT_ERR_IO:
CONN_CLOSED on read ends a close-delimited body, and a reset there must
not pass a truncated body as complete. On plain HTTP, a write refused
with CONN_CLOSED fails the request with WOLFCERT_ERR_IO and "connection
closed by peer" instead of a bare IO, like any other failed exchange.

Two codes are appended to errors.h:
- WOLFCERT_ERR_TLS_PEER_REJECTED for a received bad_certificate,
  unsupported_certificate, certificate_revoked, certificate_expired,
  certificate_unknown or unknown_ca alert, with the alert in the
  message. It is reported only for an alert that is authenticated and
  about a certificate the client had: the session negotiated TLS 1.3,
  verified the server, finished the handshake and loaded a client
  certificate. A genuine TLS 1.3 rejection follows the client's
  Finished, on a channel authenticated by the verified server. Any alert
  before that, any alert from an unverified server and any TLS 1.2 alert
  can come from anyone on the path, including after a forged TLS 1.2
  ServerHello that downgrades a TLS 1.3-capable client; such an alert
  gives WOLFCERT_ERR_TLS with "unauthenticated alert" and the alert in
  the message. access_denied and certificate_required give
  WOLFCERT_ERR_TLS even when authenticated, since a server sends them
  without having seen a certificate. Every other received fatal alert
  also gives WOLFCERT_ERR_TLS, including one that arrives on a read or
  write after the handshake, and every alert wolfSSL knows is named in
  the message. A TLS 1.2-only server's refusal is therefore
  WOLFCERT_ERR_TLS, which keeps the IDevID fallback that AGENTS.md
  describes out of an attacker's reach. Under TLS 1.3 any error alert
  counts whatever its level, as RFC 8446 section 6.2 treats them all as
  fatal.
- WOLFCERT_ERR_TLS_UNTRUSTED when the server certificate fails local
  verification: no signer, self-signed, bad signature, outside its
  validity period, path length, a chain deeper than the verify depth,
  name constraint, unknown critical extension, host name or IP mismatch,
  key usage, or a key below wolfSSL's minimum size. A signature
  algorithm or hash the wolfSSL build cannot verify keeps
  WOLFCERT_ERR_TLS, since new trust anchors cannot fix it. CRL and OCSP
  codes are left out, since wolfCert never enables either check. A trust
  anchor that wolfSSL refuses as not yet valid or expired at load
  reports it from session open, since that is how a wrong device clock
  shows up.

The mapping covers the handshake and the TLS reads and writes after it,
since a TLS 1.3 rejection arrives after wolfSSL_connect has returned.
Other failures keep their code; TLS read and write failures now carry a
message. Under OPENSSL_EXTRA, where wolfSSL reports a peer close as
SSL_ERROR_SYSCALL rather than a wolfSSL error code, that message says
the peer closed the connection. An untrusted server used to return
WOLFCERT_ERR_TLS; the session contracts in est.h, scep.h and http.h,
errors.h, AGENTS.md and EMBEDDED.md now name the new codes, say which
come from session open and which from a request, and that PEER_REJECTED
needs TLS 1.3.

test_tls_http runs the rejection against a TLS 1.2 server (ERR_TLS
naming the unauthenticated alert) and a TLS 1.3 server (PEER_REJECTED),
blocking and non-blocking, plus a TLS 1.3 server that gets no
certificate (ERR_TLS naming certificate_required) and a server that
drops the connection after the ClientHello (still IO or TLS), and
expects UNTRUSTED for a wrong trust anchor, a host name mismatch, a
server certificate checked with the clock at 2038, a 768-bit server key
and a trust anchor loaded with the clock at 1970. A TLS 1.3 refusal from
a server the client did not verify gives ERR_TLS naming the
unauthenticated alert. A raw server answering the ClientHello with a
plaintext fatal bad_certificate, alone or after a forged TLS 1.2
ServerHello, gets ERR_TLS naming the alert; a warning-level
bad_certificate and a fatal handshake_failure are never PEER_REJECTED.
Where wolfSSL has post-handshake auth, a certificate refused after a
TLS 1.3 PHA request gives PEER_REJECTED from the read. A transport that
refuses the body write with CONN_CLOSED still gets the reply and then a
closed session, and one that refuses it with IO fails the request in
wolfSSL_write. A transport with a read of its own whose write returns
CONN_CLOSED fails the request with IO at once, against a server that
never answers. Its helper no longer hangs when a session open fails
before connecting. The mTLS roundtrip expects PEER_REJECTED (ERR_TLS
without TLS 1.3) for a stale identity and for a foreign certificate on
reenroll, and test_smoke checks that every error code has a
wolfcert_strerror text.

Fixes F-18189.

@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 #75

Scan targets checked: wolfcert-src, wolfcert-bugs
Not reviewed (no scan target covers them): AGENTS.md, docs/ARCHITECTURE.md, docs/EMBEDDED.md, src/errors.c, src/http.c, src/net_posix.c
Coverage: 5 of 9 in-scope changed file(s) opened by the reviewer; not opened: wolfcert/est.h, wolfcert/http.h, wolfcert/scep.h, wolfcert/types.h

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

@Frauschi Frauschi assigned wolfSSL-Bot and unassigned Frauschi Nov 20, 2026
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