Motivation:
TLSConfiguration aims to produce an equality comparison even though it
contains closures. Unfortunately, the way it does this is subject to
some bugs. Specifically, we pass the closure directly to the generic
method `withUnsafeByes` which can in some cases force the compiler to
emit a reabstraction thunk. This happens at the point of comparison, and
so we can end up at a point where a TLSConfiguration object compares
unequal _to itself_, which is not great.
Modifications:
Defend against the reabstraction thunks by temporarily storing the
closures inside a struct, which can be passed to the generic method
without the reabstraction thunks.
Adds a helper for comparisons as well to make this more likely to get
correct implementations going forward.
Result:
Correct comparisons.
## 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>
### Motivation:
In some scenarios a server might want to accept connections from both,
authenticated and not authenticated peers and defer decisions after the
handshake. This new options allows a host to only do TLS verificaiton if
the peer presents certificates and accept connections otherwise.
### Modifications:
Add a new associated value to the `none` case of
`CertificationVerification` to maintain backwards compatibility. This
option enables opportunistic verification.
### Result:
A new configuration case for TLS verification.
---------
Co-authored-by: aryan-25 <aryan_shah@apple.com>
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 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.
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
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.
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.
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.
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.
This PR adds the support to specify what ECs the handler supports when
doing the handshake.
```swift
var config = TLSConfiguration.makeClientConfiguration()
config.curves = [.x25519]
```
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>
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.
Motivation:
Now that Swift 5.9 is GM we should update the supported versions and
remove 5.6
Modifications:
* Update `Package.swift`
* Remove `#if swift(>=5.7)` guards
* Delete the 5.6 docker compose file and make a 5.10 one
* Update docs
Result:
Remove support for Swift 5.6, add 5.10
* Drop Swift 5.5
This patch drops Swift 5.5 support. It removes docker-compose files and
deletes any conditional compilation guards that are no longer necessary.
* Remove test generation script
* Adopt `Sendable`
* `@preconcurrency typealias` doesn’t work
* `NIOPSKClient/ServerIdentityCallback`
* `NIOSSLKeyLogCallback`
* `NIOSSLClientTLSProvider`
* Fix warnings in tests
* `@preconcurrency` is broken https://github.com/apple/swift/issues/60508
I think we don’t actually need it in that case and still don’t break API
* Fix warnigns in tets
* fix nightly
Motivation
Documentation is nice, and we can help support users by providing useful
clear docs.
Modifications
Add Docc to 5.6 and later builds
Make sure symbol references work
Add overview docs
Result
Nice rendering docs
Motivation:
Add TLS-PSK Support to address #230.
Modifications:
Modified SSLContext to take a passed in PSK callback to perform key and identity operations during the handshake.
Result:
TLS-PSK support for TLS 1.2. TLS 1.3 is not addressed due to API support in BoringSSL.
Motivation:
TLSConfiguration cannot be made equatable due to its use of closures, so
we have our own best-effort functions. Unfortunately, I forgot to make
sure everything was added to them, so I'm doing so now.
Modifications:
- Add missing fields to bestEffort functions.
Result:
Better bestEffort functions.
* issue-316: Support c_rehash Directory Format
* issue-316: Address Feedback from PR
* issue-316: Addressed feedback
* issue-316: Addressed latest feedback
* issue-316: Refactor rehash check to be String.UTF8View:
* issue-316: Added split back into the String.UTF8View computational process
* issue-316: Added another split back to the computation of the rehash check
* issue-316: Added additional checks to rehash format
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:
This is an enhancement to add support for send CA names during the handshake for #231.
Modifications:
Changes to:
TLSConfiguration.swift: Added the `sendCANameList` flag to indicate loading the CA names on `.fullVerification`.
SSLContext.swift: Added the CA Names derived from the `trustRoots`.
TLSConfigurationTests.swift: Added the test case for `testFullVerificationWithCANames`.
NIOSSLTestHelpers.swift: Added a new set of PEMs for TLS and client authentication testing.
Result:
Addition for #231.
Motivation:
Currently whenever we add new config to TLSConfiguration, we add a new
static factory function that needs to provide the field. This is because
we currently allow configuration primarily by way of initializers. This
is increasingly not scaling, leading to a proliferation of almost
identical static factory functions.
We can replace this proliferation by having only "default" constructions
that default basically everything, and then having users use
getters/setters to initialize things. This also works well when we have
multiple ways of interacting with config, e.g. with the various cipher
suite operations.
Modifications:
- Deprecate all existing factory functions.
- Replace with two: forClient() and
forServer(certificateChain:privateKey:). These configure only the
mandatory things for each mode.
- Replace all uses of the deprecated initializers with the new ones,
configuring fields manually as needed.
Result:
More consistent configuration objects.
Motivation:
The new hashable implementation was missing some fields. I've added
them, and added testing. It also needed to raise the minimum NIO
version.
Modifications:
- Raised the minimum NIO version.
- Fixed the comparison.
- Added fields.
Result:
The code is right.
* Rebase: Added API for setting cipher suites in TLSConfiguration
Motivation:
Issue-207 for adding an API for setting a cipher suite while also preserving the string based declaration.
Additions to TLSConfiguration and SSLContext to allow safe access to ciphers on a context.
Modifications:
Additions to TLSConfiguration and SSLContext to allow safe access to ciphers on a context.
Added test cases in TLSConfigurationTest.
Result:
Additional API for Issue-207.
Simplified and safer access to STACK_OF(SSL_CIPHER) on a context.
* Addressed all feedback expect for memberwise init.
Motivation:
Addressed all feedback from previous feedback except for memberwise init.
Modifications:
Changes to SSLContext and TLS Configuration.
Result:
Resolved all feedback except for memberwise init.
* Addressed duplicating the memberwise init.
Motivation:
Addressing feedback for memberwise init.
Modifications:
Changes to TLSConfiguration init.
Result:
Reduced code surface.
* Added additional cipher suite tests
Motivation:
Added additional cipher suites tests per feedback.
Modifications:
Added tests to TLSConfigurationTests.
Result:
Additional cipher suite tests.
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
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:
BoringSSLs built-in list of valid signing and verification algortihms is not
always the one you actually would like to use. For example adding or removing
algorithms from this list might increase compatibility as well as security
Modifications:
Add an option to set CNIOBoringSSL_SSL_CTX_set_verify_algorithm_prefs
and CNIOBoringSSL_SSL_CTX_set_signing_algorithm_prefs
Result:
The user can set signing and verification algortihms at their will
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:
It is only possible to load multiple certificates from a PEM file,
sometimes it is useful to load multiple certificates from a buffer.
Modifications:
- Deprecate the `.file` source of loading a certificate, since it would
only load the first certificate in a file.
- Provide methods for loading multiple certificates from a PEM file or
buffer.
Result:
Multiple certificates can be loaded from a PEM file/buffer.
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:
The remote peer may not always respond to a close notify. This would
leave the SSL handler waiting to shutdown. The shutdown should timeout
in these cases to ensure shutdown completes.
Modifications:
- Added a shutdown timeout to the NIOSSLHandler, and a configurable
timeout amount to TLSConfiguration.
- Added a test to validate timing out a shutdown.
Result:
TLS shutdown will complete even if the remote peer does not respond to a
close notify.
Motivation:
In many cases it is useful to support dumping the keys used in a TLS
connection, primarily for debugging purposes. BoringSSL provides a
callback that can be used for this purpose: we should translate it into
Swift and expose it to our users.
Modifications:
- Added a keyLogCallback parameter to TLSConfiguration.
- Wired that callback up to BoringSSL.
Result:
Building blocks of SSLKEYLOGFILE support are present.
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.