Files
Rafael CepedaandAryan Shah 8d8f7844d7 Don't crash when a peer completes an optionalVerification handshake without a certificate (#590)
## Motivation

When a `NIOSSLHandler` is created with an
`additionalPeerCertificateVerificationCallback`
(via `_makeSSLServerHandler` / `_makeSSLClientHandler`) and the
`TLSConfiguration` uses
`.optionalVerification`, the peer is allowed to complete the handshake
*without* presenting
  a certificate.

In that situation `doHandshakeStep` reached the additional-verification
branch, called
`connection.getPeerCertificate()`, got `nil`, and hit a
`preconditionFailure`, aborting the
  entire process.

The constructor precondition did not catch this because
`.optionalVerification` is
`.none(validatePresentedCertificates: true)`, which is distinct from
`.none` and therefore
  passes the `!= .none` check.

  ## Modifications

  - In `NIOSSLHandler.doHandshakeStep`, only invoke the
`additionalPeerCertificateVerificationCallback` when the peer actually
presented a
certificate. The callback binding and the `getPeerCertificate()` binding
are now combined
into a single `if let ..., let ...`; if no certificate was presented the
handshake
completes normally without invoking the callback. This preserves the
previous behaviour
of only calling `getPeerCertificate()` when a callback is set
(short-circuit ordering).
  - Removed the now-unreachable `preconditionFailure`.
- Added an integration test that stands up a server with
`.optionalVerification` and an
`additionalPeerCertificateVerificationCallback`, connects a client that
presents no
certificate, and asserts the connection is handled without crashing the
process.

  ## Result

A peer that completes an `.optionalVerification` handshake without
presenting a certificate
no longer crashes the process. The additional verification callback
still runs, unchanged,
whenever a certificate *is* presented, so behaviour for the
certificate-present path is
unaffected. `.optionalVerification` +
`additionalPeerCertificateVerificationCallback` is now
a genuinely supported combination, matching what the constructor
precondition already
  advertised.

---------

Co-authored-by: Aryan Shah <aryan_shah@apple.com>
2026-08-17 14:48:31 +01:00

14 lines
128 B
Plaintext

.DS_Store
/.build
*/.build
/Packages
/*.xcodeproj
Package.pins
Package.resolved
*.pem
/docs
DerivedData
/.idea
.swiftpm
.vscode