246 Commits
Author SHA1 Message Date
Orhan Yigit Yazicilar 4a1819a281 Drop Swift 5.10 (#559)
### Motivation:

Swift 5.10 is no longer supported, we should bump the swift-tools
version.

### Modifications:

* Update Swift tools version to 6.0
* Correct the failing tests on macOS

### Result:

Code reflects our support window.
2025-09-25 13:45:48 +01:00
hamzahrmalik 6ae36f2273 Make sslContextCallback overrides replace the existing chain, rather than adding to it (#557)
Currently, the certs returned by the sslContextCallback are added to the
context, rather than replacing the certs already in the context.

This means that, if a callback is used to return a new chain to be used,
the old and new chains are both presented (but only the new leaf). This
should not cause any problem, but it is inefficient

Change: Clear the additional certs before adding the new ones
2025-09-16 15:29:14 +01:00
Gwynne Raskind 737e550e60 Make NIOSSLContext.configuration public. (#556)
Motivation:

It is sometimes useful, such as when converting between configuration
formats, to be able to recover the `TLSConfiguration` with which an
`NIOSSLContext` was created. This is not possible with the current API;
since the property is immutable, making it public poses no risk.

Modifications:

The `NIOSSLContext.configuration` property is now `public` instead of
`internal`. (Also, an extremely obsolete Linux testing note was removed
from `CONTRIBUTING.md`.)

Result:

This change has no functional impact; all behavior remains identical.
The only effect is to add the `configuration` property to the public API
of `NIOSSLContext`.
2025-08-27 10:29:08 +00:00
aryan-25 1d4afc64e8 Allow the peer's validated certificate chain to be returned in the custom verification handler (#553)
Motivation:

Users are currently able to register a custom callback in
`NIOSSLClientHandler` and `NIOSSLServerHandler` for verifiying the
certificates presented by the peer. However, there is no mechanism for
storing additional metadata for future use. This change adds support for
the peer's validated certificate chain to be returned from the custom
verification callback and later be accessed from the handler.

Modifications:

- Added a new custom verification callback type
`NIOSSLCustomVerificationCallbackWithMetadata`.
- This type is identical to the existing
`NIOSSLCustomVerificationCallback`, with the exception that callers must
complete the callback with a `NIOSSLVerificationResultWithMetadata`
(also introduced in this change). This result type can either be
initialized with no fields, or with a validated certificate chain.
- Added properties/methods to `NIOSSLHandler`, `Channel`, and
`ChannelPipeline` for accessing the validated certificate chain.

Result:

Users are now able to store the peer's validated certificate chain from
the custom verification callback and use the result downstream.
2025-08-21 12:55:53 +01:00
Cory Benfield 191e95e74c Remove incorrect documentation (#555)
Motivation:

Documentation should accurately explain the methods.

Modifications:

Removed the phrase "providing a passphrase callback"

Result:

Documentation is more accurate
2025-08-15 07:55:36 +00:00
George Barnett 385f5bd783 Avoid small behaviour change in #550 (#551)
Motivation:

In #550 a use of `NIOSSLCertificafe(file:format:)` was replaced with
`NIOSSLCertificafe.loadPEMFile(_:).first`, which isn't necessarily the
same (the error flow is different for one). To avoid any subtle
behaioral changes revert back to the old API in that one instance.

Modifications:

- Use old API

Result:

Fewer behavioural changes
2025-07-15 13:16:38 +00:00
George Barnett 5108dbdec9 Deprecate NIOSSLCertificate(file:format:) (#550)
Motivation:

NIOSSLCertificate has an 'init' which loads a single cert from PEM or
DER encoded file. This, obviously, only loads the first PEM which is
very rarely what users actually want to do. This is a common source of
error when loading cert chains and is easy to miss in review.

Modifications:

- Deprecate the init and point users to the `fromPEMBytes` method for
  PEM which returns an array of certs, and to a new `fromDERBytes` which
  returns a single cert.

Result:

Harder to misuse API
2025-07-15 13:20:55 +01:00
Rafael Cepeda f64263a41f Enabled -require-explicit-sendable compiler flag (#549)
Enabled -require-explicit-sendable compiler flag and fixed the errors
highlighted by the compiler.
2025-07-10 18:49:30 +01:00
Cory Benfield dacd828b9c Add documentation of how trust roots settings affect validators (#548)
Motivation:

The trust roots settings have sometimes got non-obvious impacts on which
validator is used. These are a legacy behaviour and so changing them is
likely to cause services to encounter challenging bugs, so it's probably
not worth fixing this behaviour without doing a deprecate-and-replace
cycle.

A deprecate-and-replace of TLSConfiguration is going to be necessary in
the near-term because it is a suboptimal structure that is carrying a
lot of load. For now, however, let's just make it possible for users to
diagnose what is going on.

Modifications:

- Document the existing behaviour

Result:

Users have a chance of figuring out what is going on.
2025-07-09 15:24:14 +01:00
Cory Benfield 8d6730f567 Add mTLSConfiguration option for TLSConfiguration (#545)
Motivation:

When configuring for mTLS, there is really only one right way to do
that. This adds that behaviour.

Modifications:

- Add mTLSConfiguration option
- Add tests

Result:

Better behaviour for mTLS.
Resolves #542
2025-07-07 10:18:27 +00:00
Rick Newton-Rogers f19bb9918d Revert "Enable certificate verification if trustRoots are set to non-nil" (#544)
Reverts apple/swift-nio-ssl#543

This may have resulted in a behavior change which would adversely affect
downstream, we should back it out whilst we investigate.
2025-07-03 16:14:23 +00:00
Cory Benfield 34486a9057 Enable certificate verification if trustRoots are set to non-nil (#543)
Motivation:

If a user sets the trust roots to a non-`none` value, they presumably
want to actually validate them. This patch enables an automatic setting
of validation if the trust roots are set.

Modifications:

- Use a didSet on TLSConfiguration to raise hostname validation when the
trustRoots are set.
- Fix up cases where the tests were doing the wrong thing

Result:

Safer APIs.
2025-07-02 17:35:14 +00:00
Cory Benfield 36b48956eb Fix incorrect callback caching (#541)
Motivation:

The certificate override callback incorrectly cached results on the
SSLContext, instead of the SSLConnection. The result is that it would
only fire once for a given SSLContext, instead of on every connection,
incorrectly caching the result indefinitely.

Modifications:

Move the callback state to SSLConnection
Write tests to validate that the callback is called the right
    number of times.

Result:

Better behaviour.
2025-06-16 14:43:38 +01:00
Gus Cairo 9173d857de Add toDerBytes to SSLPrivateKey (#540)
This PR adds new API (`SSLPrivateKey/toDerBytes()`) to get the DER bytes
for a private key.
This is only supported for native (EVP) keys; if using a custom key, an
empty array of bytes will be returned instead.
2025-06-16 11:35:42 +01:00
Egor Zhdan 43e4c3c7be Make CNIOBoringSSLShims compatible with C++ interop (#538)
This fixes linker errors such as
```
"CNIOBoringSSLShims_SSL_CTX_get_app_data(CNIOBoringSSL_ssl_ctx_st const*)", referenced from:
      static NIOSSL.NIOSSLContext.(lookupFromRawContext in <...>)(ssl: Swift.OpaquePointer) -> NIOSSL.NIOSSLContext in SSLContext.swift.o
```
when building with Swift/C++ interoperability enabled.

This is similar to https://github.com/apple/swift-nio/pull/3251.
2025-05-28 15:21:29 +01:00
Rick Newton-Rogers 4b38f35946 Drop Swift 5.9 (#536)
Motivation:

Swift 5.9 is no longer supported, we should bump the tools version and
remove it from our CI.

Modifications:

* Bump the Swift tools version to Swift 5.10
* Remove Swift 5.9 jobs where appropriate in main.yml, pull_request.yml

Result:

Code reflects our support window.
2025-05-05 12:13:41 +01:00
Cory Benfield 8061e1edd9 Add explicit API support for X25519_MLKEM768 hybrid KEM (#535)
Motivation:

While we've supported the hybrid quantum-resistant KEM for a while
(because BoringSSL has), users have had to define the constant
themselves.
This patch makes the quantum-resistant KEM part of our API.

Modifications:

- Expose x25519_MLKEM768 as a curve type.
- Add tests

Result:

More quantum-resistance.
2025-04-30 10:41:13 +01:00
Jeff DaveyandCory Benfield 874ad69680 Add subjectName, issuerName, and access to the peerCert (#533)
Motivation:

While swift-nio-ssl supports mTLS on the server, there's no api
available to get the verified X509 certificate after the handshake
completes.

Additionally, the existing API for NIOSSLCertificate doesn't have an API
to access the issuer and subject names.

Result:

These changes add new APIs. Specifically:
- nioSSL_peerCertificate() to the channel extension. Similar to
retrieving the tls version, this gets the verified peer X509 certificate
after the handshake completes.
- subjectName extension to NIOSSLCertificate. Grabs the subjectName from
the BoringSSL X509 object as an array of SSLCertificateName
- issuerName extension to the NIOSSLCertificate. Grabs the issuerName
from the BoringSSL X509 object as an array of SSLCertificateName

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2025-04-23 13:39:24 +00:00
Zane Enders 6df102a39c Fix typo in NIOSSLServerHandler documentation. (#534)
Motivation:

NIOSSLServerHandler miss named NIOSSLClientHandler in documentation.

Modifications:

Fix Typo.

Result:

Correct name in documentation.
2025-04-22 09:20:11 +00:00
Joseph Heck 66cfda0cbd Add DocC extension page to provide curation for TLSConfiguration (#532)
Motivation:

When viewing the documentation, deprecated methods clutter up available
options, sometimes below, making those options hard to find for a
developer working out how to add or create a TLSConfiguration.

Modifications:

Adds a DocC extension file (markdown format) the provides curation
(organization of the symbols) to order by task and ideally usefulness,
with deprecated methods pushed downward.

Result:

The documentation at

https://swiftpackageindex.com/apple/swift-nio-ssl/main/documentation/niossl/tlsconfiguration
(and future releases) will include this organization and be easier to
read through for developers.
2025-04-16 10:22:38 +01:00
Cory Benfield f1baaad333 Clean up Sendability issues in examples & performance tester (#528) 2025-04-11 10:13:37 +00:00
Cory Benfield a6a52bf020 Remove some redundant suppressed conformances (#522)
These suppressed conformances are not necessary, as they are redundant
with the suppressed conformance on the parent class.
2025-04-11 11:09:24 +01:00
Cory Benfield 5fe3ed5d32 Fix sendability issues in Universal Bootstrap (#527) 2025-04-11 09:19:31 +00:00
Cory Benfield a919f04adb Swap some implementation only imports to internal imports (#521) 2025-04-11 09:14:31 +00:00
Cory Benfield 1c130a82a7 Clean up Sendability errors in pipelines and promises (#524) 2025-04-11 08:59:11 +00:00
Cory Benfield f6a4469c1b Clean up sendability of statics (#523)
A number of statics in the product code aren't something the compiler
currently believes are Sendable. There are a few tweaks here:

1. Two pointer statics into BoringSSL. These are thread-safe, but the
compiler doesn't know. Mark them `nonisolated(unsafe)`.
2. One static `var` should be a static `let`.
3. One instance of reminding the compiler that closures that don't
capture anything are Sendable.
2025-04-11 08:52:42 +00:00
Cory Benfield 56bc59f677 Cleanup missing internal Sendable annotation and missing preconcurrency import (#525)
The backing storage for SubjectAlternativeNames is Sendable (it's
immutable), and Security hasn't been annotated with sendability.
2025-04-11 08:40:50 +00:00
Cory Benfield 357dd88efb Clarify that we don't hop ELs in our callbacks (#526) 2025-04-11 09:36:46 +01:00
Cory Benfield 049520bf7d Update BoringSSL to 817ab07ebb53da35afea409ab9328f578492832d (#506)
This PR updates BoringSSL to a newer commit.

Relatedly, this also lifts our minimum C++ version to C++17, to match
BoringSSL.
2025-02-10 15:43:01 +00:00
Cory Benfield 94d63e782b Actually disable hostname validation on Apple platforms when asked (#502)
Motivation:

swift-nio-ssl has long had a flag to disable its own certificate
validation behaviour. This flag works fine, and had the desired effect.

However, this flag did not take account of the fact that
Security.framework _also_ validates the hostname. Due to an
implementation bug, setting .noHostnameVerification disabled only one of
the two checks.

Modifications:

- Correctly check the flag before passing a hostname to
Security.framework
- Write some tests to make sure the behaviour works in all modes.

Result:

Better, more consistent behaviour.
2025-01-03 09:36:35 +00:00
Cory Benfield bdd5269d0c Fix issues compiling on nightly-main image (#500) 2024-12-18 11:06:08 +00:00
Cory Benfield 3cb4d5ad12 Correctly override connection state using sslContextCallback (#499)
Motivation:

The sslContextCallback overrides modify the SSL_CTX object itself.
That's not useful: in addition to having a global effect, which is
rarely what we want, certain modifications don't apply that late, and
setting the credentials is one of them. The result is that the callback
didn't actually do anything. Not that useful.

Modifications:

- Extract the cert chain setting functions from NIOSSLContext and move
them to general-purpose functions.
- Add support there for calling them on a SSL * as well as on a SSL_CTX
*
- Call them in an override function on SSLConnection
- Call that!
- Add a bunch of tests.

Result:

The callback actually does what it should.
2024-12-18 10:57:06 +00:00
Rick Newton-Rogers 4f5bd82c56 Enable MemberImportVisibility check on all targets (#497)
Enable MemberImportVisibility check on all targets. Use a standard
string header and footer to bracket the new block for ease of updating
in the future with scripts.
2024-12-17 11:08:42 +00:00
Cory Benfield e252d9bf14 Update BoringSSL to 9559c4566a6d12194c42db5f3dbbcb5de35cfec2 (#496)
This patch updates BoringSSL.
2024-12-09 16:48:49 +00:00
Cory Benfield 3ee8d53556 Update BoringSSL to 059585c8dadc7c8170c788f8c89843ee1c5b8f11 (#495) 2024-11-28 16:10:21 +00:00
Gus Cairo c7e9542133 Add API to create PKCS#12 (#486)
## Motivation
It would be handy to provide an API to create PKCS#12 files from a list
of `NIOSSLCertificates` and a `NIOSSLPrivateKey`.
This would be particularly useful when dealing with
Network.framework/NIOTransportServices/Security.framework, which use
`SecIdentity`s for SSL. Two particular use cases are
https://github.com/apple/swift-nio-ssl/issues/484#issuecomment-2410375188
and `grpc-swift-nio-transport`, which would use this API for testing the
NIOTS transport implementation.

## Modifications
This PR adds a static method to `NIOSSLPKCS12Bundle` that creates a
PKCS#12 file from the given array of certificates + private key, and
returns it as an array of bytes.

## Result
PKCS#12 files can be created using NIOSSL.
2024-10-29 14:23:36 +00:00
Rick Newton-Rogers 61cfaf9798 Migrate to GitHub Actions (#487)
Migrate CI to use GitHub Actions.

### Motivation:

To migrate to GitHub actions and centralised infrastructure.

### Modifications:

Changes of note:
* Disable warnings-as-errors until issues with recent NIO changes are
resolved
* Adopt swift-format using rules from SwiftNIO
* Remove scripts which are no longer needed

### Result:

Feature parity with old CI minus allocation tests which remain on the
old CI.
2024-10-25 09:32:23 +01:00
mads-frameo 27b25e29e7 Add support for supplying supported curves to TLSConfiguration (#485)
This PR adds the support to specify what ECs the handler supports when
doing the handshake.

```swift
var config = TLSConfiguration.makeClientConfiguration()
config.curves = [.x25519]
```
2024-10-18 15:41:46 +01:00
Cory Benfield d7ceaf0e4d Update BoringSSL to d0a175601b9e180ce58cb1e33649057f5c484146 (#483)
This patch has 3 parts: a commit to update the vendoring script, a
commit that contains the results of running the script, and then a fixup
for the API breaks we got.
2024-10-09 18:11:48 +01:00
Andrew BarbaandCory Benfield e9021d1a4e Implement SSL_CTX_set_cert_cb (#456)
This is a first pass at reviving
https://github.com/apple/swift-nio-ssl/pull/311 in order to fix
https://github.com/apple/swift-nio-ssl/issues/310

Credit to @TechnikEmpire, I did my best to address the comments in the
original PR from @Lukasa

If the approach looks okay I'll move to testing

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2024-09-16 12:26:04 +01:00
c7eef9ed36 Modify TLS PSK API to handle the case when no hint is provided by the server (#452)
Hello,

As I was trying to implement some HTTP calls using `async-http-client`
and TLS-PSK authentication, I realised that my server was not sending
any PSK hint.

The actual implementation of the clientPSKCallback makes the server psk
hint mandatory.
Similarly the implementation of the serverPSKCallback makes the server
hint mandatory.

Reading
[RFC4279](https://datatracker.ietf.org/doc/html/rfc4279#section-5.2) it
seems that on the opposite the server psk hint should be provided only
on application that mandate it.


> [5.2](https://datatracker.ietf.org/doc/html/rfc4279#section-5.2).
Identity Hint
>    In the absence of an application profile specification specifying
>    otherwise, servers SHOULD NOT provide an identity hint and clients
>    MUST ignore the identity hint field.  Applications that do use this
>    field MUST specify its contents, how the value is chosen by the TLS
>    server, and what the TLS client is expected to do with the value.

I made this PR to provide an empty string to the callbacks in the case
the server doesn't provide one.
Providing an optional might be a better solution in term of semantic but
it would break the API.

---------

Co-authored-by: Martial Lienert <mlienert@netatmo.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2024-08-22 17:01:27 +01:00
George Barnett 7b84abbdce Avoid a spin loop when inactive is fired before active (#470)
Motivation:

The NIOSSLHandler can enter a spin loop in `doUnbufferActions` if
writing data into BoringSSL fails with SSL_ERROR_WANT_READ or
SSL_ERROR_WANT_WRITE.

One way these errors can happen is if a write into BoringSSL happens
prior to the handshake completing. The NIOSSLHandler is explicit about
starting the handshake: it's done in channel active and in handler added
if the channel is already active.

However, the handshaking step is currently done without any state
checking, so if the state is 'closed' (as it would be after channel
inactive) then the handshake will still start. This can happen if
channel inactive happens before channel active.

To reach the write loop there must be a buffered write and flush prior
to the handshake step starting and the state isn't 'idle' or
'handshaking'. This can happen if a write and flush hapens while
'NIOSSLHandler' is in 'channelActive' (it forwards the 'channelActive'
event _before_ starting the handshake) and 'channelInactive' came first.

Modifications:

- Early exit from 'doHandshakeStep' if the state isn't applicable for
starting a handshake.
- Don't buffer writes when the state is 'closed' as they'll never
succeed and can be failed immediately.
- If the write isn't succesful in 'doUnbufferActions' then either write
to the network or try reading.
- Only allow a limited number of spins through 'doUnbufferActions'

Result:

- Resolves https://github.com/apple/swift-nio-ssl/issues/467
2024-08-19 10:26:01 +01:00
Cory Benfield a40a74e693 Stop using symlinks for PrivacyInfo (#475)
Motivation:

Per @jakepetroules, swift-build copies symlinks as-is into resource
bundles. That means these symlinks are dead when built with swift-build.
There's sort of no point in that.

Modifications:

Remove symlinks, replace with files.

Result:

Privacy info works.
2024-08-13 08:14:28 +01:00
Ayush Garg a9fa5efd86 Mark .file case deprecated for NIOSSLPrivateKeySource (#474)
swift-nio-ssl expects file to in specific format when loading the PrivateKey from a file baed on the extension of the file. This is prone to errors, since there is no standard agreement on the extension and format of the file. 
An example of a mismatch is `.key` swift-nio-ssl tries to load such file as DER, though these can be in PEM format too.

We could based on the binary contents of the file provided infer the format of the file, but it doesn't seem like a logic this library should own. The clients should be able to load the file themselves. 

In this PR  `.file` case is marked deprecated and relevant deprecation flags and documentation is added to point to the preferred option.
2024-07-29 03:10:26 -07:00
finagolfin 3eec6698d3 Use Bionic module and new Android overlay in Swift 6 (#468) 2024-07-15 09:52:21 +01:00
Nicolas Bachschmidt 2b09805797 Use case-insensitive comparison when validating cert domain name (#464) 2024-05-15 13:58:50 -07:00
Clinton Nkwocha 45cb3bac27 Handle error when private key file type is unknown (#460)
Motivation:

When the private key file for a TLS configuration has an unknown type, the program crashes. Hence, users are unable to catch the error.

Modifications:

A new error was added to `NIOSSLExtraError` and the program throws that error instead of crashing.

Result:

The program now throws an error instead of crashing.
2024-05-01 16:47:11 +01:00
George Barnett 2d9c63d16f Add empty privacy manifest (#459)
Motivation:

NIOSSL distributes a copy of BoringSSL, which means it needs a privacy
manifest.

Modifications:

- Add a privacy manifest

Result:

NIOSSL shouldn't be rejected from app submission by not declaring a
privacy manifest.
2024-04-09 08:04:43 +01:00
d7840f36ad fix https fail on Android (#453)
* fix https fail on Android

* add compatibility for https failure after Android14

---------

Co-authored-by: wangxiang13 <wangxiang13@lixiang.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2024-03-26 14:39:01 +00:00
George Barnett 3b2a01c9de Raise minimum Swift version to 5.8 (#458) 2024-03-11 08:50:45 +00:00