91 Commits
Author SHA1 Message Date
Johannes Weiss 584c0d06c4 universal bootstrap support (#185) 2020-03-23 16:26:05 +00:00
Cory Benfield 7add274f6e Update BoringSSL to 5298ef99bf2b2d77600b3bb74dd572027bf495be (#200)
Note that the vendor script changed: BoringSSL added a new top-level
header that had to be added to the umbrella header.
2020-03-18 08:38:46 +00:00
Matt EatonandCory Benfield 1e932f59fa Fix for Unprocessed Data before Closing (#198)
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>
2020-03-13 14:26:47 +00:00
Jake PrickettandJake Prickett c388c564ab Adjust import to allow for CocoaPod support (#192)
Co-authored-by: Jake Prickett <jpricke1@ford.com>
2020-02-26 09:37:24 +00:00
Cory Benfield b99ca3a19a Mitigate Folly/BoringSSL static linking issue. (#191)
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.
2020-02-24 14:41:27 +00:00
Matt Eaton 978e1ead99 Removed explicit Equatable implementations. (#187)
Motivation:

Finished removing explicit Equatable implementations
on BoringSSLError.

Modifications:

Removed explicit Equatable implementations.

Result:

Will allow Swift to synthesize equatable conformance for
BoringSSLError.
2020-02-21 16:19:39 +00:00
Matt Eaton f5995ca784 Replace NIOSSLError.unableToAllocateBoringSSLObject with fatalError (#186)
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.
2020-02-20 14:05:16 +00:00
Matt Eaton 6ee46effd1 Swapped applicationProtocols check for encodedApplicationProtocols. (#184)
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.
2020-02-17 08:08:26 +00:00
David Evans af46d9b58f Replace deprecated function calls (#183) 2020-02-14 12:37:30 +00:00
Cory Benfield 978dfd6c9a Update BoringSSL to 21a879a78a60c8667468a9eba994c8365eaf92ea. (#182) 2020-02-12 13:51:22 +00:00
George Barnett 9d3d664955 Avoid curried thunks (workaround SR-12115) (#176)
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.
2020-02-03 07:18:25 +00:00
Johannes Weiss cf54f5c1db enable @_implementationOnly for Swift 5.3 (#174) 2020-01-16 17:05:47 +00:00
Cory Benfield 75f7b54a5f Provide better certificate verification callback (#171)
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.
2020-01-16 15:52:35 +00:00
Cory Benfield 5d459a0ea3 Add hashability to Certificate and PrivateKey (#169)
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.
2020-01-16 12:17:31 +00:00
Cory Benfield db0e8e457e Add API for dumping certificate bytes. (#172)
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.
2020-01-14 13:08:21 +00:00
Cory Benfield 803cbead75 Update BoringSSL to 0deb91ab3f7e24307572497f0f7438684590bf92 (#167) 2020-01-07 18:02:13 +00:00
Cory Benfield b75ffaba05 Remove unsafe code from hostname verification (#166)
Motivation:

The hostname verification code operated on unsafe buffer pointers
without any clear justification for doing so. While it may have been
necessary to do that once, it certainly isn't necessary now.

Modifications:

Rewrite all the code to operate on Array.
Throw a more meaningful error.

Result:

Bounds checking, better safety.
2019-12-13 15:52:01 +00:00
Cory Benfield 48ef04e421 Better errors when we fail hostname verification (#165)
Motivation:

We've noticed that the errors you get when hostname verification fail
are a bit gross, and don't tell you much. Users deserve to know more.

Modifications:

- Added a new extensible error type.
- Threw the new error with more diagnostics after verification failed.
- Removed a weird holdover from the old days of Swift.

Result:

Better error messages.
2019-12-12 15:55:32 +00:00
George Barnett daf6e8ea04 Add missing cases to == for NIOSSLError and BoringSSLError (#163)
Motivation:

Some error types were missing cases in their `==` implementation.

Modifications:

- Add missing cases to NIOSSLError and BoringSSLError

Result:

Better equality checking
2019-12-11 09:49:34 +00:00
Cory Benfield 7b2e7cfe8c Update BoringSSL to 134fb89c4f9da7903d9aa81c6bd1d3c466782de1 (#159) 2019-12-04 12:04:20 +00:00
Cory Benfield 620a9140e8 Refactor doUnbufferWrites reduce memory pressure (#155)
Motivation:

The current implementation of doUnbufferWrites has the unfortunate habit
of both causing a substantial amount of ARC traffic and also allocating
a promise for all writes, even when one isn't necessary. Worse, if there
are many promises in a single set of flushes then we will also have to
allocate a future callback to cascade each result. All of this leads to
excessive pressure on the memory systems in Swift.

Modifications:

Refactor the code to avoid excessive use of closures, reducing ARC.
Rewrite promise chaining to reduce allocations of closure contexts and
promises.

Result:

Better performance in doUnbufferWrites
2019-12-02 10:28:52 +00:00
Cory Benfield a8dbd07f42 Add benchmarks. (#154)
Motivation:

When working with performance its useful to know what your approximate
throughput is. One of the ways we find this out in NIO is to add
benchmarks that let you know what the approximate runtime of a simple
sample program is. NIOSSL doesn't have any of these: it should.

Modifications:

Bring across the benchmark framework from other NIO projects.
Add two benchmarks mirroring the proposed allocation counter tests.

Result:

Insight into performance.
2019-12-02 09:39:02 +00:00
Cory Benfield ccf96bbe65 Add cross-compilation to symbol mangling. (#150)
Motivation:

BoringSSL potentially defines different symbols on different
architecture and platform combinations. This means that we've
historically missed mangling some symbol names when our script only
involved building on macOS.

We should build on more platforms to test this.

Modifications:

- Added support for cross compiling to Linux amd64, Linux aarch64, and
    Linux armv7 in symbol mangling script.
- Added support for compiling for iOS armv6 and arm64 in symbol mangling
    script.

Result:

Harder to run the mangling script, better results when you do.
2019-11-13 18:40:43 +00:00
Cory Benfield 63e61ed857 Update BoringSSL to 6ba98ff60144f60aba589b4d6121689528fbae76 (#149) 2019-11-13 15:04:17 +00:00
Cory Benfield e5c1af45ac Remove NIO 2.9.0 build warnings. (#144)
Motivation:

We were one of the few first-party users of
writeWithUnsafeMutableWritableBytes, and so we hit some build warnings.
We should use the nice interface to request the number of bytes we
actually want.

Modifications:

- Remove build warnings.
- Correctly ask to read a whole SSL record instead of a fragment of one.

Result:

Better code
2019-10-23 17:24:01 -07:00
Cory Benfield 37cbb1653c Clean up pointer API usage. (#143)
Motivation:

We are skirting to the edge of correctness on the pointer APIs, so we
should keep an eye on them and clean things up from time to time.

Modifications:

- Don't type cast a ByteBuffer pointer in tests.
- Use some buffer pointer APIs

Result:

Slightly better pointer usage.
2019-10-22 10:23:17 -07:00
Cory Benfield bd235c580b Update BoringSSL to 4ca15d5dcbe6e8051a4654df7c971ea8307abfe0 (#142) 2019-10-10 13:36:50 +01:00
Will Lisac 8a5135cce9 Fix BoringSSL compilation on aarch64 ARM Linux (#141) 2019-10-10 10:35:55 +01:00
Cory Benfield 109faef770 Use .note.GNU-stack section in ASM. (#137)
Motivation:

Well-formed Linux assembly code declares a .note.GNU-stack section in
order to help out the linker. When absent, the linker makes some
suboptimal linking decisions that can cause pain in running programs.

Modifications:

- Amend the vendoring script to add the .note.GNU-stack section to all
    assembly files on Linux.
- Updated BoringSSL

Results:

Better linker behaviour.
2019-10-03 18:30:58 +01:00
Cory Benfield dc2e9c0a25 Catch and namespace extra autogenerated functions. (#131)
Motivation:

Our scripts have code to catch the functions that are used with STACK_OF
and LHASH_OF data structures in BoringSSL in order to namespace them.
Sadly, some declarations were falling through the cracks: we missed some
LHASH_OF functions used internally, and we missed the
DEFINE_NAMED_STACK_OF macro used for a new C++ data type.

Modifications:

- Improved these scripts to catch more cases.
- Updated BoringSSL to 9806ae005b3085683a2fcf2953e4170893e0973d.

Result:

Fewer exported symbols from BoringSSL.
2019-08-20 13:42:56 +01:00
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
fabstu 00546ebd78 Fix compilation on iPad Pro 1st Gen with iPadOS13 and iOS 12 (#125)
Resolves an issue where the ARM architecture helpers were inadvertently excluded on 64-bit ARM platforms.
2019-08-10 15:42:30 +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
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