Motivation:
To address #253
Modifications:
When channelInactive is called during a handshake failure it currently defaults to uncleanShutdown.
This bugfix and test is to report handshakeFailed instead of uncleanShutdown.
Added the handshaking case to the channelInactive function to report handshakeFailed.
Added a new test case, testChannelInactiveDuringHandshake, in UnwrappingTests to test this fix.
Result:
Fix of #253
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:
In some cases it's useful to know when a certificate's validity dates
are. This is not always terribly straightforward to calculate, so we can
encapsulate that math within the library.
Modifications:
- Add NIOSSLCertificate.notValidBefore
- Add NIOSSLCertificate.notValidAfter
Result:
Users will be able to ask what certificate validity dates are.
Motivation:
SecTrustEvaluateAsync documents that it must be called from the same
queue it will call you back on. We failed to do this. Let's fix that up.
While we're here, we should use the non-deprecated API where it's
practical to do so.
Modifications:
- Async out to the queue before calling SecTrustEvaluateAsync.
- Call SecTrustEvaluateAsyncWithError where it's available.
Result:
Following the rules.
Motivation:
C++ structures emit a number of symbols into their binaries that are
named based on the name of the structure. Most notably, these are the
three (or five, depending on mode) magic structure methods: copy
constructor, copy assignment operator, destructor.
By and large this isn't an issue for us as BoringSSL already namespaces
most of its structures. However, a few structures are the target of
opaque typedefs in the public header files. These need to use the names
that would appear in the internal source. If they didn't, the internal
source wouldn't compile! As a result they need to appear outside the
inner prefixing namespace.
We knew about this limitation at the time we did this work, but hoped
we'd get along just fine without changing anything. In fact we did, for
nearly three years. But the chickens have come home to roost in
https://github.com/grpc/grpc-swift/issues/1134.
So it's time to fix it.
Fortunately for us, we never write down the names of the inner types in
our source code. That means we don't actually care what that name _is_.
This gives us our escape hatch: we can namespace the names of the
underlying C++ types using the same preprocessor trick as the function
names. This will have the effect of also namespacing their magic
methods.
Modifications:
This patch attempts to make this change defensively, by assuming that
other structures may be added that meet this pattern. To do it, we
compile the binary _again_ and search for any C++ methods that haven't
already been namespaced and that don't belong to Swift. We then extract
the structure name from them and add a `#define` for it. This forcibly
renames the structure in all cases.
Result:
No more colliding symbols.
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:
Using the 'class' keyword to constrain a protocol to a class is
deprecated in favour of 'AnyObject' and recent compilers emit warnings
about this.
Modifications:
- Use 'AnyObject' to constrain the 'CallbackManagerProtocol' instead
Result:
- Fewer warnings.
* Update vendoring script to fix a missing includeo
Motivation:
The openssl/evp.h header was missing causing compilation to fail.
In addition, a previously missing header is now included by BoringSSL.
Modifications:
- Update the fix import line to fix the new missing header.
Result:
BoringSSL can be vendored using our script.
* Update BoringSSL to 4a265be4d931e35f0d108040c94d37bb49827948.
Motivation:
The KeyLogCallbackManager belongs on an SSLContext, and so may be
invoked from multiple threads. However, it incorrectly cached a
ByteBuffer on its storage, leading to thread unsafety. This tends to
manifest in assertion failures, but could lead to arbitrary data
corruption or double frees.
Modifications:
- Remove the cached bytebuffer.
- Add probabilistic test.
Result:
No more thread-safety violations.
Motivation:
NIOSSLServerHandler does not have an init with
serverHostname argument, there is a typo in
deprecation message.
Modifications:
Fix a typo.
Result:
No more typo.
Motivation:
It's also 2020 in NIOSSL, and just like in NIO, there's no good reason
to use exclusionary terminology when perfectly good alternatives exist.
Modifications:
Removed exclusionary language.
Result:
Clearer codebase.
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:
In #171 when we worked on providing access to the better verification
callback, we managed to entirely miss that we had not provided that
access to servers. This meant they were stuck only with the
substantially-less-useful old-school callback, instead of the much
better new-school one.
While we're here, as we had to add multiple new initializers to
NIOSSLServerHandler, I took the opportunity to also resolve the server
handler portion of #147. The issue itself is still open because the
client handlers still have throwing inits, but all "preferred"
initializers on NIOSSLServerHandler no longer throw.
Modifications:
- Deprecated NIOSSLServerHandler.init(context:verificationCallback:)
- Implemented two new initializers on NIOSSLServerHandler.
- Added tests to verify that the NIOSSLServerHandler verification
callback is actually called.
- Removed all now-unnecessary try keywords.
Result:
Users will be able to provide custom verification callbacks that work
much better than they currently can when on the server, and the server
is now back into feature parity with the client.
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:
Unfortunately, while BoringSSL is mostly a fundamentally fantastic
library, it has inherited OpenSSL's X509 verifier. This verifier has a
number of issues, but its biggest problem is that it's simply not great
at verifying certificates!
Certificate chains are not so much "chains" as they are a single path
through a DAG. There are potentially many possible paths from a leaf
certificate to a set of roots. Some of these paths are not trusted, some
may be. BoringSSL's default verifier does not do a good job in some edge
cases because it prefers to use the intermediates provided by the client
when it could do a better job by using its own trust store.
As an example, consider the following three-certificate "chain":
CA A -- signs -- Intermediate CA B -- signs -- Leaf C
Imagine that CA A now becomes untrusted, either because it was revoked
or (more topically) because it expires. Imagine also that, in the
meantime, Intermediate CA B has become a fully-fledged CA, and has
re-issued its certificate as a self-signed root using the same key and
identifier.
The mistaken resolver may attempt to use as many certificates as
possible from the peer chain. If the peer chain is using the old,
now-replaced intermediate CA, this will cause BoringSSL to get confused:
it will build a chain that chains up to the intermediate CA, find the
old root in the trust store, and then complain that that root is expired
and fail the validation.
Instead, we want to ignore the intermediate CA B altogether! This is
because we now have a trusted root B', and if we use that then we'll
instantly get a valid chain and can move on.
This behaviour is controlled by a flag, `X509_V_FLAG_TRUSTED_FIRST`.
This flag is set by default in OpenSSL 1.1+. Sadly, it is not set by
default in BoringSSL. This patch resolves that issue.
Note that this issue only affects non-Apple platforms. On Apple
platforms we use the system resolver, which does not have this issue.
Modifications:
- Set `X509_V_FLAG_TRUSTED_FIRST`.
- Add a test to verify that flag is set.
Result:
We'll do a better (but still bad) job of building trust chains.
Motivation:
When we're failing promises on channelInactive, we do so while holding
the write buffer mutably. This means that if anyone calls write() on us,
we'll explode, as we enqueue the write into the buffer.
Modifications:
- Rewrote the close loop to not hold the write buffer mutably.
- Added a regression test.
Result:
No crashes in production.
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:
If the unwrapping timeout fires, rather than close the channel we should
just fire an error down the pipeline. This is in-line with standard NIO
behaviour, and allows users to opt-out of channel closure in the event
that they need it.
Modifications:
- Internal state modification to have different behaviour when
unwrapping and closing timeout fires.
Result:
Users can control what happens if unwrapping times out.
Motivation:
When we added TLS "unwrapping" support (that is, taking a NIOSSLHandler)
and removing it from a pipeline without killing it, we never plumbed
through the timeout behaviour from close. This can lead to permanently
delayed unwraps where the unwrap never completes.
Modifications:
Take the existing support for timed out shutdown and plumb it through to
unwrapping as well.
Result:
Unwrapping TLS cannot get stuck.
Motivation:
Fix for issue #179 that resolves error: APPLICATION_DATA_ON_SHUTDOWN
when data is present on read path while closing.
Modifications:
Minor modification to channelRead and a unit test.
Result:
Closing read paths with data will no longer error.
Data will be processed and closed gracefully.
Co-authored-by: Cory Benfield <lukasa@apple.com>
Motivation:
BoringSSL and Folly both attempt to play silly buggers with the sdallocx
symbol defined in jemalloc. Unfortunately, they don't play nicely
together, and the end result if you have both present is that BoringSSL
will reliably SIGBUS or SIGSEGV on initialization. Naturally, this isn't
good.
Modifications:
- Resolve the issue by patching out BoringSSL's allocator optimisation.
- Update BoringSSL to 6432bb46ab44731567ec923e6c8fc182f13d0070.
Result:
Flow and BoringSSL will play better together.
Motivation:
Finished removing explicit Equatable implementations
on BoringSSLError.
Modifications:
Removed explicit Equatable implementations.
Result:
Will allow Swift to synthesize equatable conformance for
BoringSSLError.
Motivation:
Provides a resolution to issue-173.
deprecate & remove throw NIOSSLError.unableToAllocateBoringSSLObject;
replace by crash #173
Modifications:
Removed all NIOSSLError.unableToAllocateBoringSSLObject and replaced
with fatalError to force a crash.
Result:
In critical points where execution should not continue a fatalError
will be thrown instead of an NIOSSLError.unableToAllocateBoringSSLObject.
Motivation:
Resolution on issue-151.
Adding a README for NIOTLSServer.
Modifications:
Swapped applicationProtocols check with encodedApplicationProtocols.
Added a README for NIOTLSServer.
Altered the README for NIOSSLHTTP1Client.
Result:
Updated documentation and swapped ALPN checks during SSLContext
initialization.
Calling `function(otherFunction)` allocates, but
`function({ otherFunction($0) }` does not.
See: SR-12115 and SR-12116.
Modifications:
Avoid passing functions directly; pass them in new closures instead.
Result:
Fewer allocations.
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:
When types are equatable its important to ask why they aren't hashable.
In the case of SSLCertificate and SSLPrivateKey there isn't any
particuarly good reason. We should support it.
Modifications:
Added Hashability to NIOSSLCertificate and NIOSSLPrivateKey.
Fixed NIOSSLPrivateKey equatable implementation.
Result:
Hashable keys and certs.
Motivation:
In some cases it's useful to be able to ask questions about what the
certificate looks like. We don't have a full X509 API, so the best way
to handle this for now is to allow users to extract the bytes of the
certificate so they can use other libraries to do the job.
Modifications:
- Added a function for dumping certificate bytes.
Result:
Users can have the certificate bytes.