Commit Graph
18 Commits
Author SHA1 Message Date
Kevin Sweeney f5dd7a60ff Don't pass read-only bytes as mutable to BoringSSL (#129)
Motivation:

The BoringSSL version of `BIO_new_mem_buf` takes a const buffer but
we pass a mutable one.

Modifications:

Pass a const buffer.

Result:

Unsafe instead of Unsafe+Mutable.
2019-08-12 16:42:17 +01:00
Kevin Sweeney c8ac6c1f5a Use CInt and CUnsignedInt when calling C APIs (#128)
Motivation:

On different platforms C may define `int` and `unsigned` to be different
types. When writing C interop code in Swift it's preferable to
use the `CInt` and `CUnsignedInt` typealiases as they will resolve to
the correct C type on all supported platforms. If the imported C API is
a definite-sized type, like `uint32_t` we leave the Swift type as
definitely-sized.

Modifications:

Use `CInt` instead of `Int32` for C `int` parameters and `CUnsignedInt`
instead of `UInt32` for C `unsigned` parameters.

Result:

Code is more portable and definite-sized types stand out.
2019-08-12 16:34:49 +01:00
Kevin Sweeney 833617d9ec Use UInt8 instead of Int8 for buffer types (#127)
Motivation:

The underlying BoringSSL BIO APIs used by NIOSSLCertificate,
NIOSSLPrivateKey take a raw byte buffer and a count,
and do not need the input buffers to be NUL-terminated. However, the API
currently takes an `Array<Int8>` (which is a typealias of `Array<CChar>` on
platforms where NIO is primarily developed). This suggests an incorrect
completion in Xcode (`utf8CString`) which includes a trailing NUL byte.
In fact, all of the NIOSSL unit tests make this mistake. The more common
use case of creating a these types from a `Data`, `ByteBuffer`, or
`String.UTF8View` is made more awkward as these are easy to convert to
`[UInt8]` but harder to convert to `[CChar]` without extending them with
an unnessary NUL byte.

Modifications:

Add new `bytes:` overloads to the convenience initializers and deprecate
the `buffer:` convenience initalizers. Use these new initializers in the
unit tests.

Result:

A more ergnomic way to correctly create these types from `Data`,
`ByteBuffer`, and `String.UTF8View`.
2019-08-12 15:46:42 +01:00
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
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
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 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
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
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
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
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