57 Commits
Author SHA1 Message Date
George Barnett 7eb0321337 Add loading of multiple certificates from a single PEM file/buffer (#123)
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.
2019-08-09 16:52:02 +01:00
George Barnett 125078ea20 Re-namespace BoringSSL from CNIOBoringSSL/ to CNIOBoringSSL_ (#122)
Motivation:

When including an xcodeproj generated by SwiftPM (which builds
CNIOBoringSSL) into an xcodeproj which wasn't generated by SwiftPM it
doesn't know about the correct header paths.

Modifications:

Update the vendor-boringssl script to:
- Namespace BoringSSL with CNIOBoringSSL_ instead of CNIOBoringSSL/
- Create an umbrealla CNIOBoringSSL header
- Apply a patch to replace inttypes.h with sys/types.h in bn.h
- Apply a patch to conditionally include arm_arch.h

BoringSSL was also updated to: ae223d6138807a13006342edfeef32e813246b39

Result:

Non-SwiftPM xcodeproj's can build a SwiftPM generated xcodeproj
which includes CNIOBoringSSL.
2019-08-09 10:23:16 +01:00
Johannes Weiss 2e85284e6a don't crash if user tried to use private key without password (#119)
Motivation:

Previously, if the user tried to open a private key that had a password
but didn't specify one, we would just crash.

Modifications:

Throw an error instead of crashing if no password was supplied but one
was needed.

Result:

Fewer crashes.
2019-08-07 12:04:46 +01:00
Cory Benfield 16de4b45c7 Update BoringSSL to a86c69888b9a416f5249aacb4690a765be064969 (#118) 2019-07-19 16:52:06 +01:00
Johannes Weiss e548c7244a workaround SR-11125: @_implementationOnly import CNIOBoringSSL (#117)
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
2019-07-17 14:10:07 +01:00
Cory Benfield 06eea9ac37 Support renegotiation-based client auth. (#111)
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.
2019-06-26 21:08:31 +01:00
Cory Benfield 77173ac903 Disable 32-bit assembly on Apple platforms. (#109)
Motivation:

The 32-bit assembly in BoringSSL doesn't assemble correctly on 32-bit
apple platforms. It took us a while to notice because we previously
haven't had any supported 32-bit platforms. NIOSSL still doesn't really
support any 32-bit Apple platforms, as all production use on those
platforms should use NIOTS, but there's no excuse for the compilation
failures really.

Modifications:

Remove assembly acceleration on 32-bit Apple platforms.

Result:

Better compilation on 32-bit Apple platforms.
2019-06-18 16:15:25 +01:00
Cory Benfield dc23a24fbe Update boringssl to ff62b38 (#108) 2019-05-22 16:41:33 +01:00
Cory Benfield 76b2883a53 Set SSL_VERIFY_FAIL_IF_NO_PEER_CERT. (#107)
Motivation:

When server users turn on certificate verification, they usually expect
that they'll actually require the certificate from their peer. Sadly,
this is not the BoringSSL default behaviour.

Modifications:

Set `SSL_VERIFY_FAIL_IF_NO_PEER_CERT` when requesting certificate
validation.

Result:

Better certificate validation.
2019-05-21 15:12:29 +01:00
Cory Benfield 18b26df5c3 Avoid using un-thread-safe static buffers when printing errors. (#102)
Motivation:

BoringSSL's docs make it pretty clear that ERR_error_string is not safe to use in a
concurrent context. Sadly, NIO is pretty damn concurrent, so we should probably stop using it.

Modifications:

- Rewrite the error printing logic to use ERR_error_string_n which takes a buffer to write
    into.

Result:

Error strings will not be corrupted in multithreaded contexts.
2019-04-17 11:31:40 +01:00
George Barnett 4f9d64ddd0 Add a timeout for TLS shutdown. (#100)
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.
2019-04-15 09:56:01 +01:00
Cory Benfield ea842bd52b Expose callback for SSLKEYLOGFILE. (#98)
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.
2019-04-12 10:40:17 +01:00
Johannes Weiss f9ac0c3486 prevent duplicate targets: rename NIOHTTP1Client to NIOSSLHTTP1Client (#99)
Motivation:

With package editing this problem surfaces:

```
$ jw-swift-5.0 swift test
error: multiple products named 'NIOHTTP1Client' in: swift-nio, swift-nio-ssl
error: multiple targets named 'NIOHTTP1Client' in: swift-nio, swift-nio-ssl
```

which is bad. Fortunately, these are executable targets that we can
rename.

Modification:

rename `NIOHTTP1Client` in swift-nio-ssl to `NIOSSLHTTP1Client`

Result:

usable with package editing.
2019-04-11 18:32:59 +01:00
Cory Benfield 842506da7b Update BoringSSL to ad9eee1628aa4dac2ac3528cb6bb5ddf27e73560. (#97)
Motivation:

We want to avoid BoringSSL falling too far out of date. This patch updates
BoringSSL.

Modifications:

Updated BoringSSL.

Result:

Newer BoringSSL.
2019-04-11 14:22:18 +01:00
George Barnett 18b8111cbf Handle close reentrancy in NIOSSLHandler. (#88)
* Handle close reentrancy in NIOSSLHandler.

Motivation:

- If the NIOSSLHandler had reads to fire during a clean shutdown, then
  they could cause an additional reentrant shutdown.
- This would fail a precondition and crash.

Modifications:

- If we're already in the shutdown state then don't perform shutdown again
- Added a test

Result:

More correct code.

* Update Linux tests
2019-03-22 09:17:50 +00:00
Cory Benfield ca35a38c94 Clean up includes that were hidden by whitespace. (#86)
Motivation:

We namespaced our header files, but some includes had weird whitespace in
the directive that we didn't notice. This meant that ARM builds failed.

Modifications:

- Became more tolerant of whitespace.
- Fixed an error in Package.swift

Result:

Better builds on ARM
2019-03-14 14:05:43 +00:00
mike_neck 5b60325b24 Provide an http client example. (#32)
* Add "/.idea" to the project's ignore file.

Motivation:

To develop with AppCode.

Modifications:

Add "/.idea" to the project's ignore file.

Result:

Add "/.idea" to the project's ignore file.

* Provide an HTTPS example. (#12)

Motivation:

To provide an example for people to use swift-nio-ssl at their projects with ease.

Modifications:

- Add NIOHTTP1Client module.

Result:

A good example for getting started with swift-nio-ssl.

* * Create OpenSSLHandler instance in channelInitializer function and chain adding handlers.

* Make sendRequest function to return EventLoopFuture and cascade promise with it to properly finish application.

* Accept command line argument.

* Make channel closed after receiving HTTPClientResponsePart.end. Because if large response is returned, channelReadComplete is called twice or more times.

* Add README.md for documenting command line argument.

* Add Connection header with handling for channelInactive

* Remove print and write bytes to stdout.

* fix compile error for NIO2.

* rename ctx to context

* dependency on NIOFoundationCompat was missing
2019-03-13 16:27:49 +00:00
Johannes Weiss d7de8f30a3 flip role boolean (#84)
Motivation:

SecPolicyCreateSSL expects a `server` parameter which is a boolean. We
previously thought it identifies what our peer is but it really
specifies what the remote peer is.

Modification:

Flip the role.

Result:

Cert validation actually works.
2019-03-13 16:01:08 +00:00
Cory Benfield 7eeb331c02 Enable TLS certificate validation on Apple platforms. (#72)
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.
2019-03-13 14:59:04 +00:00
Cory Benfield 3ae0a3b6e0 Record BoringSSL revision hash (#83)
Motivation:

When debugging, users may find it helpful to be able to nail down exactly
which version of BoringSSL is used to generate the specific release. We should
record this in two easy-to-find places: one in the Package.swift for users
going to GitHub, and one in the Sources tree for users who check the code
out.

Modifications:

- Updated the script to record the BoringSSL hash.

Result:

Easier to find out what BoringSSL release is in use.
2019-03-13 14:31:25 +00:00
Johannes Weiss 31bb183a2a NIOSSLClientHandler: require serverHostname (#82)
Motivation:

It was too easy to forget passing serverHostname to NIOSSLClientHandler
which made certificate validation fail.

Modification:

Require users to pass a server hostname or `nil` explictly.

Result:

- harder to use NIOSSLClientHandler wrongly
- fixes #80
2019-03-12 14:04:08 +00:00
Johannes Weiss 20f0d88d4d remove the use of NIO1APIShims (#81)
Motivation:

NIO1APIShims are a transitional thing that can now go away because of
convergence.

Modifications:

- remove NIO1APIShims
- require 2.0.0-convergence.1

Result:

proper dependencies
2019-03-12 13:58:42 +00:00
Cory Benfield 45fa2eb1af Namespace header-only inline functions. (#79)
Motivation:

Our previous namespacing effort used symbol analysis of a compiled binary
to locate all the symbols that needed to be namespaced away. This works
fine for regular C and C++ symbols, but fails for any header-only
inline functions.

These functions, while not emitted into the binary, can cause problems
with the C++ ODR if they are included in multiple clang modules. This
unfortunately does happen if the user includes both this library and,
for example, modularised headers that include OpenSSL headers.

Fortunately, we can namespace the inline functions as well.

Modifications:

- Namespaced all the inline headers by source analysis.

Result:

Fewer code conflicts.
2019-03-11 11:49:14 +00:00
Cory Benfield 4afbbe4539 Namespace BoringSSL header files. (#77)
Motivation:

Due to the way SwiftPM builds projects it's possible for our
BoringSSL header files to clash with other people's OpenSSL header
files. That leads to build errors and sadness.

Modifications:

- Updated the BoringSSL vendor script to move all header files.
- Updated script to point C files at new header files.
- Brought in new BoringSSL version.

After your change, what will change:

Happier users
2019-03-06 16:21:01 +00:00
Tanner 1d550836c9 move uncleanShutdown error to NIOSSLError (#76) 2019-03-06 09:47:03 +00:00
Cory Benfield 45b1f82692 Rename everything. (#75)
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.
2019-03-01 17:25:33 +00:00
Cory Benfield ba433215c3 Removed vestigial CNIOOpenSSL module. (#74)
Motivation:

OpenSSL went away a long time ago, we don't need to keep it around.

Modifications:

Deleted the old CNIOOpenSSL module source.

Result:

Less unnecessary source code.
2019-03-01 14:58:22 +00:00
Cory Benfield ed92b42be5 Rename SSLContext to NIOSSLContext (#73)
Motivation:

The name SSLContext conflicts with a type in Security.framework. That's
a really unnecessary naming clash, and while it's unlikely to be a real
problem in most programs, we still shouldn't encourage that if we can
avoid it.

Modifications:

- Rename SSLContext to NIOSSLContext.

Result:

Fewer naming conflicts.
2019-03-01 12:54:58 +00:00
Cory Benfield f63c57657a Locate default ca-certificates on Linux. (#71)
Motivation:

When we moved away from the system copy of OpenSSL, we lost the
compiled-in locations of the default root CAs. We should go looking for
them so that certificate verification still works!

Modifications:

- Added a runtime one-off search to locate the roots.

Result:

Certificate verification works on Linux
2019-02-28 10:48:11 +00:00
Tanner 9d46aef76b make TLSConfiguration.applicationProtocols a [String] (#70)
* add separate encodedApplicationProtocols method to TLSConfiguration

* store application protocols in encoded format
2019-02-28 09:55:35 +00:00
Johannes Weiss f531a05167 update to latest NIO 2 (#69)
Motivation:

Code should compile and have the latest NIO 2 API.

Modifications:

- rename `ctx` to `context`
- apply all fixits

Result:

compiles again
2019-02-26 13:03:17 +00:00
Cory Benfield 141ff46685 Use BoringSSL for TLS. (#59)
Motivation:

Requiring the system's SSL library was a constant source of problems. 

Modifications:

This is the patch that transitions swift-nio-ssl to use BoringSSL, as discussed in [the Swift forums](https://forums.swift.org/t/rfc-moving-swiftnio-ssl-to-boringssl/18280).

Result:

Easier SSL support
2019-02-25 16:52:26 +00:00
Cory Benfield 11b78f6f9e Clean up now-unnecessary warnings. (#61)
Motivation:

As we bumped the dependency of NIO to 1.12, we can now avoid some
deprecation warnings, as even the low-end of NIO versions we
support will be safe.

While we're here, we can remove the warnings that Swift 5 started
emitting for our visibility specifiers in extensions.

Modifications:

- Replaced changeCapacity with reserveCapacity
- Replaced numThreads with numberOfThreads
- Removed redundant visibility specifiers

Result:

Back to warning free compilation.
2019-01-09 17:21:30 +00:00
Cory Benfield f0e77e00a4 Support removing TLS handlers from live connections. (#54)
Motivation:

In some unusual protocol scenarios it is desirable to remove a TLS
tunnel from an active connection. Currently NIOOpenSSL does not
support doing this: the only way to get it to issue a CLOSE_NOTIFY
is to call close() on the Channel. This is less than ideal.

Modifications:

- Added a OpenSSLHandler.stopTLS method
- Reworked the internal state machine to handle this use-case.

Result:

Users can safely unwrap TLS from a Channel.
2018-12-10 11:39:40 +00:00
Johannes Weiss 31d06047aa make TLSConfiguration members changeable (#58)
Motivation:

TLSConfiguration had all its members as constants (`let`) which was only
making users lives harder than necessary.

Modifications:

make all TLSConfiguration properties `var`

Result:

happier users
2018-11-26 08:11:30 +00:00
Cory Benfield 085af4c63b Prevent crashes when closing twice. (#52)
Motivation:

If a user called close on a channel without attaching a promise, and
subsequently called close again with a promise, before the TLS shutdown
completes, this would lead to a crash due to a force-unwrap. It has
been my experience that crashes are less than ideal, especially when
users have done nothing wrong.

Modifications:

Replaced the force-unwrap with code that correctly persists the promise.
Added a test.

Result:

Less likely to crash.
2018-11-07 11:30:53 +00:00
Cory Benfield 081068d62f Support extracting public keys in handshakes. (#49)
Motivation:

Users have requested the ability to extract information about the
public keys provided by peers during TLS handshakes. This is a reasonable
request: it's potentially very useful to have access to this information.

While I'm reluctant to add too much in the way of general cryptographic
functionality, it seems like a good idea to enable it for others. For
this reason, this patch extends NIOOpenSSL to enable this use case.

Modifications:

1. Added an OpenSSLPublicKey class that can be serialized to bytes.
2. Added support for obtaining an OpenSSLPublicKey from an
    OpenSSLCertificate.
3. Added support for setting the OpenSSL cert verification callback.

Result:

Much more flexibility and power in the library.
2018-11-05 13:16:09 +00:00
Albert Aleksieiev db16c3a90b fix Android support (#45)
### Android support

**Motivation:**
* fix `swift-nio-ssl` compatible with Android. This is a continuation(apple/swift-nio#609) of porting `Vapor/WebSocket` and all related packages to Android.
* remove unused #if macros for Android
* fix `stat` func with convention(c) attribute for Android

**Modifications:**
fix `FileSystemObject::pathType` and `sysStat` Android API inconsistency

**Result:**
now `swift-nio-ssl` compatible with Android!
2018-10-05 14:08:21 +01:00
Cory Benfield 8380fa29a2 Make importing CNIOOpenSSL safer. (#41)
Motivation:

There are a number of dangerous portions of CNIOOpenSSL that make it
tricky to use. This mostly doesn't manifest in debug mode, or in this
module, but when compiling in release mode or when others use this module
it can all go subtly wrong.

We should make this module easier to use correctly.

Modifications:

- Remove the constant defined in the header file, replace with #define.
- Move all static inline declarations to regular function declarations.

Result:

Fewer compile errors, easier to use this module.
2018-09-18 13:09:53 +02:00
Cory Benfield 9b03e08e42 Flush buffered reads when we read a CLOSE_NOTIFY. (#40)
Motivation:

When we read a CLOSE_NOTIFY message, we immediately begin closing the
connection down. This *can* cause an instant close, and if that occurs
then we will have un-flushed data in the read buffer that we never
deliver.

If we receive a CLOSE_NOTIFY we will not receive more application data,
so we should deliver all that application data before we initiate the
shutdown process.

Modifications:

Unbuffer the buffered reads when we read a CLOSE_NOTIFY.

Result:

All reads will be shipped before channelInactive can fire.
2018-09-18 12:39:08 +02:00
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