17 Commits
Author SHA1 Message Date
Cory Benfield 3f487a1658 Support configuring TLS 1.3 cipher suites. (#38)
Motivation:

OpenSSL uses orthogonal configuration options for pre- and post- TLS 1.3
cipher suites. This is because the two are quite unlike each other, and
so it helps to configure them separately.

Users are therefore currently only able to configure the pre-TLS 1.3
cipher suites. We should allow them to configure the post-TLS1.3 ones too.

Modifications:

Added new configuration options for TLS 1.3 cipher suites.

Result:

Configuration of TLS 1.3 cipher suites will be possible.
2018-09-17 17:18:21 +02:00
Cory Benfield 8d994592e3 Get tests passing with OpenSSL 1.1.1. (#36)
Motivation:

As OpenSSL 1.1.1 has just been released, it is reasonable for users to
be able to use it with SwiftNIO.

Modifications:

- Fixed an issue where we would fail to exclude TLSv1.3 when users set
  max TLS version to lower than TLSv1.3.
- Fixed an issue where we'd fail to read application data sent in the
  same TCP segment as a Finished message.
- Fixed a minor typo in error message strings.

Result:

OpenSSL 1.1.1 is formally supported.
2018-09-17 10:31:55 +01:00
Cory Benfield 1bf8870915 Support OpenSSL 1.1 and later. (#20)
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.
2018-09-13 09:27:35 +02:00
Cory Benfield 9e7e47c1ca Add custom ByteBufferBIO object. (#27)
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.
2018-08-17 11:52:02 +01:00
Cory Benfield 0b0bf35497 Improve performance characteristics of TLS echo server. (#28)
Motivation:

The TLS echo server was doing a few stupid things it didn't need to.
That was greatly slowing it down when testing raw throughput, and also
demonstrating bad coding style.

Modifications:

- Stopped sending promises for writes we don't care about.
- Flushed less frequently, only on channelReadComplete.
- Didn't shrink maxMessagesPerRead

Result:

Better performance from the TLS echo server.
2018-08-16 14:26:18 +01:00
Cory Benfield 6617eb0d3a Move syscalls to a port of Posix enum. (#24)
Motivation:

Right now we make a bunch of naked syscalls that don't correctly
handle reporting errno or catching EINTR. We can't use the NIO Posix
enum to fix this for us because it's internal, and because it doesn't
have the function we need. Instead, we create a temporary port of those
functions.

Ideally we'd reconcile these at some time in the future.

Modifications:

Add a port of the Posix enum and its core functions.
Improve wrapErrorIsNullReturnCall to be more useful.
Adopt the posix functions.

Result:

Better safety in the face of EINTR.
Better reporting of errnos.
2018-07-12 12:26:00 +01:00
Cory Benfield f9a2581ceb Correctly keep buffers alive. (#22)
Motivation:

BIO_new_mem_buf does not take a copy of its underlying memory, so it's
enormously important that the pointer we pass it remain valid for the
duration of the lifetime of the BIO. This was previously not actually
something we did. Very bad.

Modifications:

Extended the scope of the `withUnsafeBytes` call until the `BIO` created
from those bytes is supposed to go out of scope.

Result:

Better memory safety.
2018-07-12 11:50:34 +01:00
Cory Benfield c5e345b62e Add support for PKCS#12 bundles. (#23)
Motivation:

As requested in #17, it's not uncommon to want to support PKCS#12 bundles.
This patch adds support for unpacking PKCS#12 bundles into useful data types
for NIOOpenSSL.

Modifications:

Added new OpenSSLPKCS12Bundle type.
Added new fromUnsafePointer(takingOwnership:) static functions to
    OpenSSLCertificate and OpenSSLPrivateKey.
Deprecated fromUnsafePointer(pointer:) on OpenSSLCertificate and
    OpenSSLPrivateKey.

Result:

PKCS#12 bundles can be used to distribute certs/keys for SwiftNIO.
2018-07-12 11:48:28 +01:00
Cory Benfield c0e3f285bc Support private keys with passphrases. (#21)
Motivation:

Many users want to use passphrases to secure their private keys. We have not
previously supported any mechanism to provide that passphrase other than
the default OpenSSL passphrase callback. This is not useful in all
circumstances, and is particularly troubling in the case of automated
systems. We should add better support.

Modifications:

Enhance OpenSSLPrivateKey to allow setting a passphrase callback.
Enhance SSLContext to allow setting a passphrase callback.

Result:

Users can set passphrase callbacks.
2018-07-09 16:26:45 +01:00
Cory Benfield 8e0aa765e3 Set SSL_MODE_RELEASE_BUFFERS and SSL_MODE_AUTO_RETRY. (#14)
Motivation:

There are two key issues at play here.

Firstly, OpenSSL connections by default hang on to buffers for idle
connections. This isn't really necessary, and setting
SSL_MODE_RELEASE_BUFFERS can save enormous amounts of memory (tens of kb
per connection in some cases).

Secondly, and more profoundly, OpenSSL's default behaviour on SSL_read
is unexpected. In particular, it is possible to get SSL_read to return
SSL_ERROR_WANT_READ in cases where the receive BIO does actually have
an application data record in it, but where a non-application data
record was received first.

Handling that is a tricky case, but it's much easier to ask OpenSSL to
do it for us by just setting SSL_MODE_AUTO_RETRY. This really ought to
be the default behaviour: the fact that it isn't is fairly perplexing.

See https://github.com/openssl/openssl/issues/6234 for more discussion
of SSL_MODE_AUTO_RETRY.

Modifications:

Set SSL_MODE_RELEASE_BUFFERS and SSL_MODE_AUTO_RETRY.

Result:

Less memory usage, fewer subtle bugs.
2018-05-14 21:32:14 +01:00
Joe Smith 38955a5f80 Include md5 openssl Header (#13)
Motivation:

I'm switching [an AWS SDK](https://github.com/noppoMan/aws-sdk-swift) to use NIO under the hood, and there are some MD5 sum operations we need to complete related to requests + file operations.

Including [the `md5.h` header ](https://github.com/libressl/libressl/blob/master/src/crypto/md5/md5.h) will make it much easier to utilize and prevent requiring an additional package to get  at the functions already present here.

Modifications:

Included the `md5.h` header in `CNIOOpenSSL

Result:

This will allow md5 related functions to be used by packages including `CNIOOpenSSL`.
2018-04-30 11:25:11 +01:00
Cory Benfield fb3bc221d6 Remove processing of now-nonexistent flush promises. (#6)
Motivation:

There are no more flush promises in NIO, so we don't need to bend
over backwards to track them appropriately.

Modifications:

Remove the code that handles flush promises. Simplify.

Result:

Simpler, easier to follow code for buffered writes.
2018-02-23 11:03:40 +01:00
Johannes Weiss 66863edf2a adopt latest API 2018-02-21 18:56:19 +00:00
Cory Benfield 46b3a8448c Update to newer breaking change of NIO 2018-02-14 09:23:25 +00:00
Cory Benfield bcc121011b Fix compiles on Swift 4.1
Without this change we break on Swift 4.1 on Linux due to
SR-6963.
2018-02-09 09:50:17 +00:00
Cory Benfield 044b978525 Bring up-to-date with NIO latest 2018-02-08 16:50:53 +00:00
Johannes Weiß a6936a36f1 move OpenSSL support from NIO main repo 2018-01-25 18:35:14 +00:00