148 Commits
Author SHA1 Message Date
Cory Benfield 52a486ff6d Update BoringSSL to 295b31324f8c557dcd3c1c831857e33a7f23bc52 2022-01-11 08:55:02 +00:00
Cory Benfield 2b329e37e9 Update BoringSSL to 2042972e8458833714bce23386931b1c79978439 (#334) 2021-12-09 06:24:01 -08:00
Cory Benfield 0324d60429 Update BoringSSL to 3a667d10e94186fd503966f5638e134fe9fb4080 2021-11-25 17:30:53 +00:00
Cory Benfield 90101e7a15 Provide better error on EOF during handshake (#332)
Motivation:

When we get channelInactive during a handshake, we currently produce a
fairly useless error:
.handshakeFailed(NIOSSL.BoringSSLError.sslError([])). This error isn't
helpful to anyone, and it doesn't really communicate what's happened.

Sadly, due to the tightly constrained types it's really hard for us to
add new errors to handshakeFailed. Regardless, it would be good for us
to give as much diagnostic information as we can here.

Modifications:

- Changed the representation of BoringSSLInternalError to allow
  "synthetic" errors that don't come from BoringSSL at all.
- Added a new synthetic error for eof during handshake.
- Sent that error instead of a BoringSSL error stack that will always be
  empty.

Result:

- Easier to diagnose the error.
2021-11-25 13:46:38 +00:00
Cory Benfield 36f6419f2b Trust additional roots and defaults on Darwin
Motivation:

When we added additional trust roots on Darwin, we didn't add much
testing to cover that use-case because testing the default validator is
hard. This meant a bug slipped through: we called a pair of APIs in the
wrong order. As a result, users using additional trust roots on Darwin
_only_ trusted the additional ones: the defaults were unavailable.

Modifications:

- Fix the call so that we trust both the defaults and the additional
  roots.
- Refactor the security framework validator to make it possible to test
  it directly.
- Add unit tests for the security framework validator.

Result:

We'll correctly trust the additional roots and the defaults on Darwin.
2021-11-12 17:45:37 +00:00
Cory Benfield 08e701df9a Add x509_vfy.h to umbrella header 2021-11-10 09:09:23 +00:00
Cory Benfield 4b7979a723 Update BoringSSL to a9670a8b476470e6f874fef3554e8059683e1413 2021-11-10 09:09:23 +00:00
buttaface d5494402cd Fix build for Android armv7 (#328)
Motivation:

Fix compilation error because of a type mismatch between st_mode and mode_t on
32-bit Android.

Modifications:

- Zero-extend mode_t constants on Android armv7 to match the types.
- Scope an earlier modification in this vein to armv7 only.

Result:

The code builds on Android armv7 again.
2021-10-13 07:40:14 +01:00
Matt Eatonandmeaton2 21e2918756 issue-316: Support c_rehash Directory Format (#327)
* issue-316: Support c_rehash Directory Format

* issue-316: Address Feedback from PR

* issue-316: Addressed feedback

* issue-316: Addressed latest feedback

* issue-316: Refactor rehash check to be String.UTF8View:

* issue-316: Added split back into the String.UTF8View computational process

* issue-316: Added another split back to the computation of the rehash check

* issue-316: Added additional checks to rehash format

Co-authored-by: meaton2 <meaton3@apple.com>
2021-10-11 10:09:16 +01:00
Justin Leger 5e68c1ded1 Adding back the Explicit time_t import (#325) 2021-09-23 11:14:05 +01:00
Cory Benfield 15d2090ae3 Tolerate zeroReturnError in closed/unwrapped (#323)
Motivation:

In some rare cases we've seen production applications crash due to us
getting a zeroReturnError in either closed or perhaps unwrapped. This is
pretty hard to get to trigger in realistic situations, but I've found a
contrived-but-possible way to hit it in tests. In both of these cases we
are able to simply ignore the error and move on.

Modifications:

- Added a test to replicate the issue
- Tolerate .unwrapped and .closed on zeroReturnError.

Result:

Fewer weird crashes
2021-09-21 15:43:17 +01:00
Cory Benfield 70da47e2d4 Add callback-based custom private keys. (#322)
Motivation:

NIOSSL traditionally handles private keys be requiring that they can be
decoded into a BoringSSL EVP_PKEY structure. This requires the key to
have an in-memory representation that BoringSSL understands, and
ultimately forces the bytes of the private key into memory.

In many cases, however, this is either undesirable or not possible.
For security reasons it is common to want to offload a private key into
secure storage, such as the iPhone's Secure Enclave or a portable smart
card. In other circumstances it may be useful to put the private key on
an entirely different machine and have the signing operations occur over
an RPC mechanism.

In all of these cases it is necessary to have a programmatic interface
to the private key that can be implemented in an essentially arbitrary
manner.

Modifications:

- Defined a new protocol, `NIOSSLCustomPrivateKey`, that allows users to
  implement the private key operations needed for TLS.
- Plumbed support for this interface through `NIOSSLPrivateKey`.
- Added support for the BoringSSL interface to `SSLConnection`.

Result:

Custom TLS private keys backed by function calls can now be provided to
NIOSSL.
2021-09-21 12:57:22 +01:00
Cory Benfield 7ced2aed37 Remove the implementationOnly dance (#321)
Motivation:

When we added implementationOnly imports a while back, we supported
Swift 5.0, and so we needed to guard their availability behind a
compiler check. We only support 5.2 and later now, so that check is
redundant and can go away.

Modifications:

- Removed compiler checks around imports of our C libraries.

Result:

Cleaner code
2021-09-16 09:29:08 +01:00
Cory Benfield c359ba56f7 Clean up imports and dependencies (#319)
Motivation:

With NIO 2.32.0 we broke the core NIO module up into modules that split
apart the POSIX layer and the core abstractions. As a result, this
package no longer needs to express a hard dependency on the POSIX layer.

Modifications:

- Rewrote imports of NIO to NIOCore.
- Added NIOEmbedded and NIOPosix imports where necessary in tests.
- Extended soundness script to detect NIO imports.
- Note that the main modules still depend on NIO, which is necessary
for backwards-compatibility reasons. This dependency is unused.

Result:

No need to use NIOPosix.
2021-09-14 14:55:25 +01:00
Cory Benfield 688f902407 Update BoringSSL to 25773430c07075a368416c3646fa4b07daf4968a (#315)
* Update BoringSSL to 25773430c07075a368416c3646fa4b07daf4968a

* Clean up the use of the X509 pointer

Motivation:

BoringSSL has begun opaquifying their X.509 library. This unfortunately
manifests as source code changes in Swift due to the way OpaquePointer
behaves. As a result, we need to change the places that held an X.509
pointer directly.

While we're here, we've been playing a bit fast-and-loose with the
lifetime of owned pointers. We should take this opportunity to clean
things up and be a bit better behaved.

Modifications:

- Replace the X509 pointers with OpaquePointer
- Use scoped access to the X.509 access outside the NIOSSLCertificate
  class.

Result:

Code compiles with better pointer hygeine.
2021-09-09 08:54:01 +01:00
Matt Eaton 2e74773972 issue-231: Adding CA Names to the TLS handshake. (#307)
Motivation:

This is an enhancement to add support for send CA names during the handshake for #231.

Modifications:

Changes to:
TLSConfiguration.swift: Added the `sendCANameList` flag to indicate loading the CA names on `.fullVerification`.
SSLContext.swift: Added the CA Names derived from the `trustRoots`.
TLSConfigurationTests.swift: Added the test case for `testFullVerificationWithCANames`.
NIOSSLTestHelpers.swift: Added a new set of PEMs for TLS and client authentication testing.

Result:

Addition for #231.
2021-09-08 16:37:12 +01:00
Cory Benfield 0119cf84a0 Update BoringSSL to 2e68a05c9943a8dec1758d4a393b2ae906fd3295 (#308)
Also tweak the vendor script to add an explicit destination when
building for iOS.
2021-08-17 09:37:51 +01:00
buttaface bca7b6e6d4 Android: FILE is an opaque struct since API 24 (#306)
Motivation:

Get this Swift file building for Android.

Modifications:

Use an OpaquePointer for FILE* on Android.

Result:

This file builds and all the tests pass on Android.
2021-07-22 15:29:51 +01:00
Cory Benfield 9c457d021e Rationalise TLSConfiguration construction. (#299)
Motivation:

Currently whenever we add new config to TLSConfiguration, we add a new
static factory function that needs to provide the field. This is because
we currently allow configuration primarily by way of initializers. This
is increasingly not scaling, leading to a proliferation of almost
identical static factory functions.

We can replace this proliferation by having only "default" constructions
that default basically everything, and then having users use
getters/setters to initialize things. This also works well when we have
multiple ways of interacting with config, e.g. with the various cipher
suite operations.

Modifications:

- Deprecate all existing factory functions.
- Replace with two: forClient() and
  forServer(certificateChain:privateKey:). These configure only the
  mandatory things for each mode.
- Replace all uses of the deprecated initializers with the new ones,
  configuring fields manually as needed.

Result:

More consistent configuration objects.
2021-05-14 08:52:26 +01:00
Johannes Weiss d09bd494a8 docs: warn about NIOSSLContext being expensive to create 2021-05-13 12:29:13 +01:00
Cory Benfield 33a4041c09 Clean up new hashable implementation. (#298)
Motivation:

The new hashable implementation was missing some fields. I've added
them, and added testing. It also needed to raise the minimum NIO
version.

Modifications:

- Raised the minimum NIO version.
- Fixed the comparison.
- Added fields.

Result:

The code is right.
2021-05-13 10:09:34 +01:00
Matt EatonandJohannes Weiss 0ca62dd739 Rebase: Added API for setting cipher suites in TLSConfiguration (#293)
* Rebase: Added API for setting cipher suites in TLSConfiguration

Motivation:

Issue-207 for adding an API for setting a cipher suite while also preserving the string based declaration.
Additions to TLSConfiguration and SSLContext to allow safe access to ciphers on a context.

Modifications:

Additions to TLSConfiguration and SSLContext to allow safe access to ciphers on a context.
Added test cases in TLSConfigurationTest.

Result:

Additional API for Issue-207.
Simplified and safer access to STACK_OF(SSL_CIPHER) on a context.

* Addressed all feedback expect for memberwise init.

Motivation:

Addressed all feedback from previous feedback except for memberwise init.

Modifications:

Changes to SSLContext and TLS Configuration.

Result:

Resolved all feedback except for memberwise init.

* Addressed duplicating the memberwise init.

Motivation:

Addressing feedback for memberwise init.

Modifications:

Changes to TLSConfiguration init.

Result:

Reduced code surface.

* Added additional cipher suite tests

Motivation:

Added additional cipher suites tests per feedback.

Modifications:

Added tests to TLSConfigurationTests.

Result:

Additional cipher suite tests.

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2021-05-11 14:57:20 +01:00
Helge Heß 6363cdf6d2 Explicit time_t import (#294)
W/o this `time_t` is only available as an implicit import via `CNIOBoringSSL`, which is `@_implementionOnly`.

The issue popped up in `swift-lambda`: https://github.com/SwiftXcode/swift-lambda/issues/6

I suspect it is due to slightly different import ordering (which makes the compiler pickup the alias). It probably is not specific to SPMDestinations, the import order is just undefined?
2021-05-11 08:54:45 +01:00
b2f8f35326 Implement bestEffort functions for hashable and equatable. (#280)
* Make TLSConfiguration Hashable

* Fix equatable conformance

* Add basic tests

* Remove hash not equal check

* Implement bestEffort functions

* add doc

* add test for different callbacks

* generate tests

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2021-05-05 09:30:24 +01:00
Cory Benfield 3d576964a1 Update BoringSSL to ab7811ee8751ea699b22095caa70246f641ed3a2 (#289) 2021-04-22 11:16:54 +01:00
Matt Eaton e1f6ac1c45 Bug fix to replace uncleanShutdown with handshakeFailed during TLS failure. (#287)
Motivation:

To address #253

Modifications:

When channelInactive is called during a handshake failure it currently defaults to uncleanShutdown.
This bugfix and test is to report handshakeFailed instead of uncleanShutdown.

Added the handshaking case to the channelInactive function to report handshakeFailed.
Added a new test case, testChannelInactiveDuringHandshake, in UnwrappingTests to test this fix.

Result:

Fix of #253
2021-04-08 08:24:31 +01:00
Cory Benfield 955f3d2e7f Manage early-exit of ByteBufferBIO in tests. (#286)
Motivation:

Nightly Swift's improvement to the copy propagation pass has caused
ByteBufferBIOTests to start misbehaving, as they now deallocate
some ByteBufferBIOs very early in the test case. This is because the
backing BIO pointer does not have a strong reference to the
ByteBufferBIO.

Modifications:

- Give the backing BIO a strong ref that can be broken by the
    ByteBufferBIO calling close().
- Update the tests to call close()

Result:

No leaking BIOs.
2021-04-07 16:57:17 +01:00
Si Beaumont c4f0dbe924 Add support for additional TLS trust roots (#284)
Until now `TLSConfiguration` has support for specifying trust roots
using _one_ of the following:

* Path to file or directory with certificates, or
* A list of `NIOSSLCertificate` the caller has in their hand, or
* The system default.

It's potentially useful to specify a combination of these, e.g. the
system default _and_ some additional provided certificates.

This patch adds a new field, `TLSConfiguration.additionalTrustRoots`
which will be used in combination with the existing
`TLSConfiguration.trustRoots`.

`TLSConfigurationTest` needed the following changes:
* Stop using `connectInMemory` for testing successful handshakes on
  Darwin because it is not thread-safe when used with a verify callback.
* Add Extended Key Usage extenstions to the keys used in tests to
  satisfy the stricter requirements of the Security.framework verifier.
2021-03-29 09:43:21 +01:00
Cory Benfield 58b355e5dc Provide notValidBefore and notValidAfter. (#282)
Motivation:

In some cases it's useful to know when a certificate's validity dates
are. This is not always terribly straightforward to calculate, so we can
encapsulate that math within the library.

Modifications:

- Add NIOSSLCertificate.notValidBefore
- Add NIOSSLCertificate.notValidAfter

Result:

Users will be able to ask what certificate validity dates are.
2021-03-17 13:08:10 +00:00
Cory Benfield 4e7e996e3c Call SecTrustEvaluateAsync from correct queue. (#281)
Motivation:

SecTrustEvaluateAsync documents that it must be called from the same
queue it will call you back on. We failed to do this. Let's fix that up.
While we're here, we should use the non-deprecated API where it's
practical to do so.

Modifications:

- Async out to the queue before calling SecTrustEvaluateAsync.
- Call SecTrustEvaluateAsyncWithError where it's available.

Result:

Following the rules.
2021-03-16 08:38:04 +00:00
Cory Benfield 6b6c0582d6 Update boringssl to 04b3213d43492b6c9e0434d8e2a4530a9938f958 (#279) 2021-03-10 14:31:02 +00:00
Cory Benfield 5cf978934a Update vendor script to namespace C++ structures. (#269)
Motivation:

C++ structures emit a number of symbols into their binaries that are
named based on the name of the structure. Most notably, these are the
three (or five, depending on mode) magic structure methods: copy
constructor, copy assignment operator, destructor.

By and large this isn't an issue for us as BoringSSL already namespaces
most of its structures. However, a few structures are the target of
opaque typedefs in the public header files. These need to use the names
that would appear in the internal source. If they didn't, the internal
source wouldn't compile! As a result they need to appear outside the
inner prefixing namespace.

We knew about this limitation at the time we did this work, but hoped
we'd get along just fine without changing anything. In fact we did, for
nearly three years. But the chickens have come home to roost in
https://github.com/grpc/grpc-swift/issues/1134.

So it's time to fix it.

Fortunately for us, we never write down the names of the inner types in
our source code. That means we don't actually care what that name _is_.
This gives us our escape hatch: we can namespace the names of the
underlying C++ types using the same preprocessor trick as the function
names. This will have the effect of also namespacing their magic
methods.

Modifications:

This patch attempts to make this change defensively, by assuming that
other structures may be added that meet this pattern. To do it, we
compile the binary _again_ and search for any C++ methods that haven't
already been namespaced and that don't belong to Swift. We then extract
the structure name from them and add a `#define` for it. This forcibly
renames the structure in all cases.

Result:

No more colliding symbols.
2021-02-12 08:23:29 +00:00
Cory Benfield f6726f800c Update BoringSSL to bb43a45d6de7375f3310511d37f040d1055f8a10 (#268) 2021-02-03 12:55:20 +00:00
Cory Benfield ae5f9a3840 Stop guarding implementationOnly (#266)
Motivation:

When we added support for implementationOnly imports, we guarded them
behind a maximum compiler version to prevent the possibility of the
spelling breaking in a future compiler release. This was pessimistic and
largely unnecessary. If the spelling does change in a future compiler
release, we can always ship a patch update that adds the compiler guard
to fix older versions. The solution, as it stands, will require trivial
patches every six months to maintain, which is much messier than just
removing the upper bound.

Modifications:

- Remove the upper bound on the compiler version for
  `@_implementationOnly` import.

Result:

Fewer trivial patches.
2021-01-25 14:16:23 +00:00
Fabian Fett 00c264d7a5 Use welcoming language (#265) 2021-01-22 13:46:59 +00:00
George Barnett 6dcfa09717 Use 'AnyObject' for class-constrained protocols (#264)
Motivation:

Using the 'class' keyword to constrain a protocol to a class is
deprecated in favour of 'AnyObject' and recent compilers emit warnings
about this.

Modifications:

- Use 'AnyObject' to constrain the 'CallbackManagerProtocol' instead

Result:

- Fewer warnings.
2021-01-15 13:10:47 +00:00
Cory Benfield 62bf5083df Update BoringSSL to df75139becd7355e117c9860452265b898b49376. (#261) 2020-12-16 09:34:00 +00:00
George Barnett 901a014233 Fixup vendoring script and update BoringSSL. (#256)
* Update vendoring script to fix a missing includeo

Motivation:

The openssl/evp.h header was missing causing compilation to fail.
In addition, a previously missing header is now included by BoringSSL.

Modifications:

- Update the fix import line to fix the new missing header.

Result:

BoringSSL can be vendored using our script.

* Update BoringSSL to 4a265be4d931e35f0d108040c94d37bb49827948.
2020-11-27 11:15:57 +00:00
David Evans eb102ad32a Update boring ssl (#249) 2020-10-14 15:25:02 +01:00
Cory Benfield 55f8a1a581 Fix threading violation in KeyLogCallbackManager (#250)
Motivation:

The KeyLogCallbackManager belongs on an SSLContext, and so may be
invoked from multiple threads. However, it incorrectly cached a
ByteBuffer on its storage, leading to thread unsafety. This tends to
manifest in assertion failures, but could lead to arbitrary data
corruption or double frees.

Modifications:

- Remove the cached bytebuffer.
- Add probabilistic test.

Result:

No more thread-safety violations.
2020-10-14 14:35:12 +01:00
David Evans 9852198a15 Add NIOSSLCertificate serial number var, and add to description (#247)
* Add serial number

* Make a lazy var

* PR comments

* PR comments
2020-10-14 08:28:41 +01:00
David EvansandCory Benfield 7c403e7d14 Add NIOSSLCertificate debug description for useful debugging (#246)
* Add debug description

* Fix test

* Linux tests

* Update Sources/NIOSSL/SSLCertificate.swift

Co-authored-by: Cory Benfield <lukasa@apple.com>

* Update Sources/NIOSSL/SSLCertificate.swift

Co-authored-by: Cory Benfield <lukasa@apple.com>

* Update Sources/NIOSSL/SSLCertificate.swift

Co-authored-by: Cory Benfield <lukasa@apple.com>

* Address review comments

* Move IP address to it's own extension

* Add preconditions

* PR comments

* Fix Swift 5.0

* Pr comments

* Update Sources/NIOSSL/SSLCertificate.swift

Co-authored-by: Cory Benfield <lukasa@apple.com>

* Update Sources/NIOSSL/SSLCertificate.swift

Co-authored-by: Cory Benfield <lukasa@apple.com>

* Update Sources/NIOSSL/SSLCertificate.swift

Co-authored-by: Cory Benfield <lukasa@apple.com>

* Update Sources/NIOSSL/SSLCertificate.swift

Co-authored-by: Cory Benfield <lukasa@apple.com>

* Update Sources/NIOSSL/SSLCertificate.swift

Co-authored-by: Cory Benfield <lukasa@apple.com>

* Update Sources/NIOSSL/SSLCertificate.swift

Co-authored-by: Cory Benfield <lukasa@apple.com>

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-10-13 16:59:13 +01:00
Cory Benfield cc0e80dbcc Update BoringSSL to 67818bea6690a230e2f42e8a588e0f54949bbbf1 (#244) 2020-09-30 13:27:21 +01:00
Artem Redkin 8f53c5e059 Fixes a typo in deprecation message. (#240)
Motivation:
NIOSSLServerHandler does not have an init with
serverHostname argument, there is a typo in
deprecation message.

Modifications:
Fix a typo.

Result:
No more typo.
2020-09-08 15:00:58 +01:00
George Barnett ea1dfd6419 Update BoringSSL to 1c58648f14ed75f2a8cc3ae08897987d97f493ec (#239)
* Update patch-2-arm-arch.patch

* Update BoringSSL to 1c58648f14ed75f2a8cc3ae08897987d97f493ec
2020-08-26 16:17:22 +01:00
Cory Benfield 8a137b72a9 Update BoringSSL to 54858b63c1d886f6c8d903d4a4f594f1485de189 (#235) 2020-07-24 11:43:48 +01:00
Cory Benfield 3a012b89cf Fix inappropriate language in NIOSSL (#233)
Motivation:

It's also 2020 in NIOSSL, and just like in NIO, there's no good reason
to use exclusionary terminology when perfectly good alternatives exist.

Modifications:

Removed exclusionary language.

Result:

Clearer codebase.
2020-07-10 08:53:16 +01:00
Matthias Bartelmeß b94f2b6297 Add option to specify signining and verification algorithms (#232)
Motivation:

BoringSSLs built-in list of valid signing and verification algortihms is not
always the one you actually would like to use. For example adding or removing
algorithms from this list might increase compatibility as well as security

Modifications:

Add an option to set CNIOBoringSSL_SSL_CTX_set_verify_algorithm_prefs
and CNIOBoringSSL_SSL_CTX_set_signing_algorithm_prefs

Result:

The user can set signing and verification algortihms at their will
2020-07-09 15:47:13 +01:00
Cory Benfield 5a6ad123af Add support for custom verify callback to servers. (#226)
Motivation:

In #171 when we worked on providing access to the better verification
callback, we managed to entirely miss that we had not provided that
access to servers. This meant they were stuck only with the
substantially-less-useful old-school callback, instead of the much
better new-school one.

While we're here, as we had to add multiple new initializers to
NIOSSLServerHandler, I took the opportunity to also resolve the server
handler portion of #147. The issue itself is still open because the
client handlers still have throwing inits, but all "preferred"
initializers on NIOSSLServerHandler no longer throw.

Modifications:

- Deprecated NIOSSLServerHandler.init(context:verificationCallback:)
- Implemented two new initializers on NIOSSLServerHandler.
- Added tests to verify that the NIOSSLServerHandler verification
  callback is actually called.
- Removed all now-unnecessary try keywords.

Result:

Users will be able to provide custom verification callbacks that work
much better than they currently can when on the server, and the server
is now back into feature parity with the client.
2020-06-16 16:01:42 +01:00
Cory Benfield f0b118d9af Allow double-setting the custom verify callback. (#224)
Motivation:

An unnecessary assertion prevented us double-setting the custom verify
callback. This had the effect of making the custom verify callback
unusable on Apple platforms in debug mode only. The actual code
tolerates a double-setting of the custom verify callback just fine, but
the assertion prevents it for no good reason.

Modifications:

- Removed an assertion.
- Added some tests to confirm it's all fine.

Result:

Users won't get crashes when they try to use the custom verify callback.
Fixes #223.
2020-06-11 12:26:37 +01:00