28 Commits
Author SHA1 Message Date
Cory Benfield 86458a336b Update BoringSSL to 8eb25be6fa18d475291122887a7bccd3762ff236 (#607)
As a standard pattern, the vendoring script needed some changes, as did
our internal code.

The biggest change here is that BoringSSL has changed its name mangling
scheme. This causes us to no longer need to explicitly write the prefix
out anymore: in fact, doing that causes compiler errors. So this patch
rewrites the code to resolve that issue.

Along the way, I've also added some checks for making sure the name
mangling is correct.
2026-10-09 17:19:47 +00:00
Allison df9c340602 Set custom private keys via sslContextOverride (#573)
Motivation:

NIOCertificateReloading provides updated versions of a certificate and
private key pair, in the form of a NIOSSLContextConfigurationOverride,
to be used when performing a TLS handshake in NIO. Prior to this change,
the Swift customPrivateKey callback did not see the override. It always
read the private key from the immutable TLSConfiguration if one was
provided at initialization. This configuration might have been nil and
resulted in a precondition failure if the user was expecting to set the
key on the first reloader run. Otherwise, it would have contained a
stale key.

Modifications:
- Store the NIOSSLContextConfigurationOverride in SSLConnection when
applyOverride() is called.
- Update the customKey computed property to check the stored override
first and fall back to the parent context.
- Add tests:
- Server configuration sets a native key, reloader switches to a custom
key for signing.
- Server configuration sets a custom key, reloader switches to a native
key for signing.
- Server configuration sets a native key, reloader switches to a custom
key for decryption.
    - Client key and cert are set by reloader.

Result:

NIOCertificateReloading will work as expected with custom private keys.
2026-03-06 11:56:02 +00:00
Cory Benfield c1bcde58f6 Revert to @_implementationOnly import (#562)
Motivation:

Unfortunately, `internal import` doesn't do implementation-hiding in the
build system. This means that the C header files do become visible to
all dependencies, which can cause problems when the ssl.h headers from
elsewhere are included.

Modifications:

Revert to `@_implementationOnly` imports.

Result:

Better builds, unfortunately more warnings
2025-10-28 15:52:53 +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
Cory Benfield a919f04adb Swap some implementation only imports to internal imports (#521) 2025-04-11 09:14:31 +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 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
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
Cory Benfield 7aabfa3b64 Add support for shrinking massive ByteBufferBIOs (#443)
Motivation:

ByteBufferBIOs will grow their outbound buffer until flush is
called. They will retain their capacity at however large it got
to avoid reallocating, as they assume that your write pattern will
be fairly consistent.

In some cases this is a bad strategy, because it overcommits data.
To allow users to reclaim this data, we can add a configuration
option that will shrink the playload on the next write.

Modifications:

- Added support for ByteBufferBIO to shrink the payload.
- Plumbed up support for handler configuration.
- Tests that shrinkage actually works.

Result:

Users can reclaim memory.
2023-09-18 14:11:27 +01:00
Cory Benfield 70649f8c93 Tolerate massive writes (#384)
Motivation:

In rare cases, users may attempt to write more data than can be
expressed in 2**31 bytes. This should not crash our program.

Modifications:

- Break up extremely long writes into smaller chunks
- Make sure to clamp on the read path too

Result:

Users who send hilariously long writes can do so.
2022-07-26 14:03:03 +01:00
Matt Eatonandmeaton2 8abde3ea82 Enhancement to address issue-337 to query the final TLSVersion (#338)
* Enhancement to address issue-337 to query the final `TLSVersion`

Motivation:

To provide an enhancment to resolve issue-337 to query the final `TSLVersion`.
This allows a client channel to grab the final TLSVersion used for the connection.

Modifications:

Added extension points to allow querying the `TLSVersion` from the `SSLConnection` based on the `Channel` and `ChannelPipeline`.
Added an `unknown` type to `TLSVersion` to account for a default case where the `TLSVersion` cannot be derived from the conenction.
Added a unit test `testObtainingTLSVersionOnClientChannel` to query the final `TLSVersion` when the client channel is closed.

Result:

2 APIs added to query the `TLSVersion` when on a channel.

Additional Notes:

Note that I have 2 concerns about this PR that I would like some additional feedback on:
1. First is the `unknown` case for `TLSVersion`.  This may not be needed as BoringSSL may just return `tlsv12` each time the version cannot be derived from the connection.  Let me know your thoughts.
2. I am not wild about the way this is being unit-tested, but I wanted to do it this way because it provides the `TLSVersion` on the `Channel` after it is closed.  Let me know your thoughts.

* Removed unknown case as it cases an API breakage.

Motivation:

Removed unknown case as it cases an API breakage.

Modifications:

Defaulted to tlsv12 as this is what BoringSSL will default to.

Result:

Wiped out unknown and switched to tlsv12 as the default

* issue--337: Fix failure on soundness.sh for Linux test

* issue-337: Addressed feedback from review

* issue-337: Updated PR to address feedback

* issue-337: Updated the EventLoopFuture for the Channel extension

* issue-337: Refactored test case

Co-authored-by: meaton2 <meaton3@apple.com>
2022-01-20 10:20:49 +00:00
Cory Benfield 70da47e2d4 Add callback-based custom private keys. (#322)
Motivation:

NIOSSL traditionally handles private keys be requiring that they can be
decoded into a BoringSSL EVP_PKEY structure. This requires the key to
have an in-memory representation that BoringSSL understands, and
ultimately forces the bytes of the private key into memory.

In many cases, however, this is either undesirable or not possible.
For security reasons it is common to want to offload a private key into
secure storage, such as the iPhone's Secure Enclave or a portable smart
card. In other circumstances it may be useful to put the private key on
an entirely different machine and have the signing operations occur over
an RPC mechanism.

In all of these cases it is necessary to have a programmatic interface
to the private key that can be implemented in an essentially arbitrary
manner.

Modifications:

- Defined a new protocol, `NIOSSLCustomPrivateKey`, that allows users to
  implement the private key operations needed for TLS.
- Plumbed support for this interface through `NIOSSLPrivateKey`.
- Added support for the BoringSSL interface to `SSLConnection`.

Result:

Custom TLS private keys backed by function calls can now be provided to
NIOSSL.
2021-09-21 12:57:22 +01:00
Cory Benfield 7ced2aed37 Remove the implementationOnly dance (#321)
Motivation:

When we added implementationOnly imports a while back, we supported
Swift 5.0, and so we needed to guard their availability behind a
compiler check. We only support 5.2 and later now, so that check is
redundant and can go away.

Modifications:

- Removed compiler checks around imports of our C libraries.

Result:

Cleaner code
2021-09-16 09:29:08 +01:00
Cory Benfield c359ba56f7 Clean up imports and dependencies (#319)
Motivation:

With NIO 2.32.0 we broke the core NIO module up into modules that split
apart the POSIX layer and the core abstractions. As a result, this
package no longer needs to express a hard dependency on the POSIX layer.

Modifications:

- Rewrote imports of NIO to NIOCore.
- Added NIOEmbedded and NIOPosix imports where necessary in tests.
- Extended soundness script to detect NIO imports.
- Note that the main modules still depend on NIO, which is necessary
for backwards-compatibility reasons. This dependency is unused.

Result:

No need to use NIOPosix.
2021-09-14 14:55:25 +01:00
Cory Benfield 955f3d2e7f Manage early-exit of ByteBufferBIO in tests. (#286)
Motivation:

Nightly Swift's improvement to the copy propagation pass has caused
ByteBufferBIOTests to start misbehaving, as they now deallocate
some ByteBufferBIOs very early in the test case. This is because the
backing BIO pointer does not have a strong reference to the
ByteBufferBIO.

Modifications:

- Give the backing BIO a strong ref that can be broken by the
    ByteBufferBIO calling close().
- Update the tests to call close()

Result:

No leaking BIOs.
2021-04-07 16:57:17 +01:00
Si Beaumont c4f0dbe924 Add support for additional TLS trust roots (#284)
Until now `TLSConfiguration` has support for specifying trust roots
using _one_ of the following:

* Path to file or directory with certificates, or
* A list of `NIOSSLCertificate` the caller has in their hand, or
* The system default.

It's potentially useful to specify a combination of these, e.g. the
system default _and_ some additional provided certificates.

This patch adds a new field, `TLSConfiguration.additionalTrustRoots`
which will be used in combination with the existing
`TLSConfiguration.trustRoots`.

`TLSConfigurationTest` needed the following changes:
* Stop using `connectInMemory` for testing successful handshakes on
  Darwin because it is not thread-safe when used with a verify callback.
* Add Extended Key Usage extenstions to the keys used in tests to
  satisfy the stricter requirements of the Security.framework verifier.
2021-03-29 09:43:21 +01:00
Cory Benfield ae5f9a3840 Stop guarding implementationOnly (#266)
Motivation:

When we added support for implementationOnly imports, we guarded them
behind a maximum compiler version to prevent the possibility of the
spelling breaking in a future compiler release. This was pessimistic and
largely unnecessary. If the spelling does change in a future compiler
release, we can always ship a patch update that adds the compiler guard
to fix older versions. The solution, as it stands, will require trivial
patches every six months to maintain, which is much messier than just
removing the upper bound.

Modifications:

- Remove the upper bound on the compiler version for
  `@_implementationOnly` import.

Result:

Fewer trivial patches.
2021-01-25 14:16:23 +00:00
Cory Benfield f0b118d9af Allow double-setting the custom verify callback. (#224)
Motivation:

An unnecessary assertion prevented us double-setting the custom verify
callback. This had the effect of making the custom verify callback
unusable on Apple platforms in debug mode only. The actual code
tolerates a double-setting of the custom verify callback just fine, but
the assertion prevents it for no good reason.

Modifications:

- Removed an assertion.
- Added some tests to confirm it's all fine.

Result:

Users won't get crashes when they try to use the custom verify callback.
Fixes #223.
2020-06-11 12:26:37 +01:00
Johannes WeissandCory Benfield e0049a1600 @_implementationOnly also for Swift 5.3 (#212)
Motivation:

@_implementationOnly also works in Swift 5.3, let's use it.

Modification:

Use @_implementationOnly for Swift 5.1 ... 5.3

Result:

Proper 5.3 support.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-05-01 08:01:28 +01:00
Johannes Weiss cf54f5c1db enable @_implementationOnly for Swift 5.3 (#174) 2020-01-16 17:05:47 +00:00
Cory Benfield 75f7b54a5f Provide better certificate verification callback (#171)
Motivation:

We've exposed the OpenSSL certificate verify callback for a while now.
Unfortunately, this callback is fairly hostile to callers, as it is
called repeatedly through the certificate verification process, instead
of presenting the unified certificate chain to the user.

BoringSSL has a better verification callback available that we use
internally, but that is not exposed to users. It would be nice to give
users the better API.

Modifications:

- Expose the BoringSSL verification callback to users.
- Re-plumb the Security.framework verification to use our public API.

Result:

Easier certificate chain verification overrides.
2020-01-16 15:52:35 +00:00
Cory Benfield 48ef04e421 Better errors when we fail hostname verification (#165)
Motivation:

We've noticed that the errors you get when hostname verification fail
are a bit gross, and don't tell you much. Users deserve to know more.

Modifications:

- Added a new extensible error type.
- Threw the new error with more diagnostics after verification failed.
- Removed a weird holdover from the old days of Swift.

Result:

Better error messages.
2019-12-12 15:55:32 +00:00
Cory Benfield e5c1af45ac Remove NIO 2.9.0 build warnings. (#144)
Motivation:

We were one of the few first-party users of
writeWithUnsafeMutableWritableBytes, and so we hit some build warnings.
We should use the nice interface to request the number of bytes we
actually want.

Modifications:

- Remove build warnings.
- Correctly ask to read a whole SSL record instead of a fragment of one.

Result:

Better code
2019-10-23 17:24:01 -07:00
Kevin Sweeney c8ac6c1f5a Use CInt and CUnsignedInt when calling C APIs (#128)
Motivation:

On different platforms C may define `int` and `unsigned` to be different
types. When writing C interop code in Swift it's preferable to
use the `CInt` and `CUnsignedInt` typealiases as they will resolve to
the correct C type on all supported platforms. If the imported C API is
a definite-sized type, like `uint32_t` we leave the Swift type as
definitely-sized.

Modifications:

Use `CInt` instead of `Int32` for C `int` parameters and `CUnsignedInt`
instead of `UInt32` for C `unsigned` parameters.

Result:

Code is more portable and definite-sized types stand out.
2019-08-12 16:34:49 +01:00
Johannes Weiss e548c7244a workaround SR-11125: @_implementationOnly import CNIOBoringSSL (#117)
Motivation:

If we don't import CNIOBoringSSL @_implementationOnly, then some details
of libssl's C structs might leak into NIOSSL's ABI which might lead to
clashes further down the line.

Modification:
- make all CNIOBoringSSL(Shims) imports @_implementationOnly
- remove all concrete libssl types from instance variables

Result:
- fixes #116
- works around SR-11125
2019-07-17 14:10:07 +01:00
Cory Benfield 06eea9ac37 Support renegotiation-based client auth. (#111)
Motivation:

While TLS renegotiation is in general a bad idea, it is widely used for
delayed TLS-based client authentication. We are able to support this
use-case, and so we should enable it.

As with BoringSSL, we'd like to continue to default to not supporting
this mode of operation. However, users are free to enable support if
they so choose.

Modifications:

- Added flags for configuring renegotiation.
- Added integration test harness.
- Added renegotiation integration test with s_server.
- Added renegotiation and client cert support to sample client.

Result:

Users will be able to perform TLS renegotiation.
2019-06-26 21:08:31 +01:00
Cory Benfield 7eeb331c02 Enable TLS certificate validation on Apple platforms. (#72)
Motivation:

While our main use-case is users on Linux, many of our users develop on
their Mac. We need to support their development model, which may well
involve making outbound requests. Those requests need certificate
validation.

Modifications:

- Added routines for using Security.framework to validate cert chains.
- Refactored code to be as platform-independent as possible.

Result:

Certificate verification works on Apple platforms.
2019-03-13 14:59:04 +00:00
Cory Benfield 45b1f82692 Rename everything. (#75)
Motivation:

Unfortunately, it turns out that the word "OpenSSL" just ended up everwhere
in our codebase, including in the names of our types. This was, in hindsight,
a bit of an unfortunate bit of naming.

We should take this breaking opportunity to give these types better names.
In particular, we should stop identifying them as belonging to any library
other than this one, "NIOSSL". That way we become somewhat independent of
the specific way that this library is implemented.

Modifications:

Renamed a bunch of stuff. Internal types mostly got their names changed
from OpenSSL->BoringSSL. External types generally got their names changed
to "NIOSSL", except where those types directly reflected a BoringSSL
implementation detail, specifically in the errors.

The main name changes are:

SSLContext -> NIOSSLContext
OpenSSLError -> BoringSSLError
NIOOpenSSLError -> NIOSSLError
OpenSSLInternalError -> BoringSSLInternalError
OpenSSLPKCS12Bundle -> NIOSSLPKCS12Bundle
OpenSSLPassphraseCallback -> NIOSSLPassphraseCallback
OpenSSLPassphraseSetter -> NIOSSLPassphraseSetter
OpenSSLPrivateKey -> NIOSSLPrivateKey
OpenSSLPublicKey -> NIOSSLPublicKey
OpenSSLCertificate -> NIOSSLCertificate
OpenSSLVerificationResult -> NIOSSLVerificationResult
OpenSSLVerificationCallback -> NIOSSLVerificationCallback
OpenSSLCertificateSource -> NIOSSLCertificateSource
OpenSSLPrivateKeySource -> NIOSSLPrivateKeySource
OpenSSLTrustRoots -> NIOSSLTrustRoots
OpenSSLSerializationFormats -> NIOSSLSerializationFormats
OpenSSLHandler -> NIOSSLHandler
OpenSSLClientHandler -> NIOSSLClientHandler
OpenSSLServerHandler -> NIOSSLServerHandler

Oh, and renamed the module too: it's now NIOSSL, instead of NIOOpenSSL.

Result:

Users have better names for things.
2019-03-01 17:25:33 +00:00