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.
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.
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
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.
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.
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.
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.
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.
* 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>
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.
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
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.
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.
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.
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.
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.
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>
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.
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.
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
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.
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
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.
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.
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.