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.
This commit is contained in:
Cory Benfield authored and GitHub committed 2020-06-11 12:26:37 +01:00
1 parent e14f8ebd6a
commit f0b118d9af
3 files changed
+77 -1

No files matched your search

+2 -1
View File
@@ -164,10 +164,11 @@ internal final class SSLConnection {
// Store the verification callback. We need to do this to keep it alive throughout the connection.
// We'll drop this when we're told that it's no longer needed to ensure we break the reference cycles
// that this callback inevitably produces.
assert(self.customVerificationManager == nil)
self.customVerificationManager = callbackManager
// We need to know what the current mode is.
// Note that this also has the effect of ensuring that if we disabled certificate validation
// it actually _stays_ disabled: if the verify mode is no-verification, this callback never gets called.
let currentMode = CNIOBoringSSL_SSL_get_verify_mode(self.ssl)
CNIOBoringSSL_SSL_set_custom_verify(self.ssl, currentMode) { ssl, outAlert in
guard let unwrappedSSL = ssl else {
@@ -52,6 +52,8 @@ extension NIOSSLIntegrationTest {
("testErroringNewVerificationCallback", testErroringNewVerificationCallback),
("testNewCallbackCanDelayHandshake", testNewCallbackCanDelayHandshake),
("testExtractingCertificatesNewCallback", testExtractingCertificatesNewCallback),
("testNewCallbackCombinedWithDefaultTrustStore", testNewCallbackCombinedWithDefaultTrustStore),
("testMacOSVerificationCallbackIsNotUsedIfVerificationDisabled", testMacOSVerificationCallbackIsNotUsedIfVerificationDisabled),
("testRepeatedClosure", testRepeatedClosure),
("testClosureTimeout", testClosureTimeout),
("testReceivingGibberishAfterAttemptingToClose", testReceivingGibberishAfterAttemptingToClose),
@@ -1534,6 +1534,79 @@ class NIOSSLIntegrationTest: XCTestCase {
XCTAssertEqual(certificates, [NIOSSLIntegrationTest.cert])
}
func testNewCallbackCombinedWithDefaultTrustStore() throws {
// This test is mostly useful on macOS, where it previously failed due to an excessive assertion.
let serverConfig = TLSConfiguration.forServer(certificateChain: [.certificate(NIOSSLIntegrationTest.cert)],
privateKey: .privateKey(NIOSSLIntegrationTest.key))
let clientConfig = TLSConfiguration.forClient(certificateVerification: .fullVerification, trustRoots: .default)
let serverContext = try assertNoThrowWithValue(NIOSSLContext(configuration: serverConfig))
let clientContext = try assertNoThrowWithValue(NIOSSLContext(configuration: clientConfig))
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
XCTAssertNoThrow(try group.syncShutdownGracefully())
}
let serverChannel: Channel = try serverTLSChannel(context: serverContext, handlers: [], group: group)
defer {
XCTAssertNoThrow(try serverChannel.close().wait())
}
let handshakeCompletePromise = group.next().makePromise(of: Void.self)
let customCallbackCalledPromise = group.next().makePromise(of: Void.self)
let clientChannel = try clientTLSChannel(context: clientContext,
preHandlers: [],
postHandlers: [WaitForHandshakeHandler(handshakeResultPromise: handshakeCompletePromise)],
group: group,
connectingTo: serverChannel.localAddress!,
serverHostname: "localhost",
customVerificationCallback: { _, promise in
// Note that we override certificate verification here.
customCallbackCalledPromise.succeed(())
promise.succeed(.certificateVerified)
})
defer {
XCTAssertNoThrow(try clientChannel.close().wait())
}
XCTAssertNoThrow(try customCallbackCalledPromise.futureResult.wait())
XCTAssertNoThrow(try handshakeCompletePromise.futureResult.wait())
}
func testMacOSVerificationCallbackIsNotUsedIfVerificationDisabled() throws {
// This test is mostly useful on macOS, where it validates that disabling verification actually, well,
// disables verification.
let serverConfig = TLSConfiguration.forServer(certificateChain: [.certificate(NIOSSLIntegrationTest.cert)],
privateKey: .privateKey(NIOSSLIntegrationTest.key))
let clientConfig = TLSConfiguration.forClient(certificateVerification: .none, trustRoots: .default)
let serverContext = try assertNoThrowWithValue(NIOSSLContext(configuration: serverConfig))
let clientContext = try assertNoThrowWithValue(NIOSSLContext(configuration: clientConfig))
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
XCTAssertNoThrow(try group.syncShutdownGracefully())
}
let serverChannel: Channel = try serverTLSChannel(context: serverContext, handlers: [], group: group)
defer {
XCTAssertNoThrow(try serverChannel.close().wait())
}
let handshakeCompletePromise = group.next().makePromise(of: Void.self)
let clientChannel = try clientTLSChannel(context: clientContext,
preHandlers: [],
postHandlers: [WaitForHandshakeHandler(handshakeResultPromise: handshakeCompletePromise)],
group: group,
connectingTo: serverChannel.localAddress!,
serverHostname: "localhost")
defer {
XCTAssertNoThrow(try clientChannel.close().wait())
}
// This connection should succeed, as certificate verification is disabled.
XCTAssertNoThrow(try handshakeCompletePromise.futureResult.wait())
}
func testRepeatedClosure() throws {
let serverChannel = EmbeddedChannel()
let clientChannel = EmbeddedChannel()