Motivation:
Currently there are no Swift.org distributions of Swift for Linux that
link against an OpenSSL 1.1 release, but with the recent release of
Ubuntu 18.04, the next LTS release of Ubuntu, it seems reasonable that
at some point such a release will exist.
Given that OpenSSL 1.1 is substantially better than OpenSSL 1.0, we
should endeavour to allow our users to take advantage of this new support
and compile against this newer OpenSSL.
*Unfortunately*, OpenSSL's specific source change is not well-tolerated
by Swift code. There are a number of reasons for this, mostly explained
in https://forums.swift.org/t/opaque-pointers-in-swift/6875. The end
result of this is that we require substantial, wide-ranging changes to the
implementation in order to successfully compile against both OpenSSL
versions 1.0.X and 1.1.X.
This patch contains those wide ranging modifications. The most profound
of them is to use the compiler to essentially convert between arbitrary
pointer types without writing the names of those types down.
Unfortunately this totally escapes the Swift type system, which is
substantially less than ideal. However, in the current mode where opaque
C structures are unityped in Swift it is really the best we can do.
Modifications:
- Supported converting all pointer types to OpaquePointer.
- Added conversions absolutely everywhere.
- Fixed a few problems with callbacks.
- Moved OpenSSL initialization to C to support both macros and
functions.
- Made locking callbacks optional.
- Fixed tests that relied on OpenSSL 1.0.2 behaviour of ALPN failure
to now check OpenSSL version before they assume an outcome.
Result:
Compile against OpenSSL 1.1.0 at the very least.
Motivation:
Currently we use OpenSSL with MemoryBIO objects. These objects are an
implementation of OpenSSL's pervasive BIO abstraction that wrap a simple
heap-allocated buffer.
The result of this is that we have the following data pipeline for
reads:
1. Ciphertext read from the socket into ByteBuffer
2. Ciphertext copied from ByteBuffer to memory BIO
3. Ciphertext copied from memory BIO to transient OpenSSL memory
4. Ciphertext decrypted
5. Plaintext copied from transient OpenSSL memory to ByteBuffer.
And for writes:
1. Plaintext copied from ByteBuffer to transient OpenSSL memory.
2. Plaintext encrypted.
3. Ciphertext copied from transient OpenSSL memory to memory BIO.
4. Plaintext copied from memory BIO to ByteBuffer.
5. Plaintext copied from ByteBuffer to kernel.
The thing is, we don't need all of these copies. Specifically, the
interposing of a memory BIO is totally unnnecessary. We can remove
that copy by implementing the BIO abstraction ourself on top of the
ByteBuffer objects we're using.
This patch does just that.
Modifications:
- Implement a new ByteBufferBIO object.
- Rewrite some core code to use ByteBufferBIO.
Result:
Less memory copying, better efficiency.