From f0b118d9af6c4e78bc4f3f4fbb464020172b0bf4 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Thu, 11 Jun 2020 12:26:37 +0100 Subject: [PATCH] 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. --- Sources/NIOSSL/SSLConnection.swift | 3 +- .../NIOSSLIntegrationTest+XCTest.swift | 2 + Tests/NIOSSLTests/NIOSSLIntegrationTest.swift | 73 +++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) diff --git a/Sources/NIOSSL/SSLConnection.swift b/Sources/NIOSSL/SSLConnection.swift index 3b178c79..aa676dfa 100644 --- a/Sources/NIOSSL/SSLConnection.swift +++ b/Sources/NIOSSL/SSLConnection.swift @@ -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 { diff --git a/Tests/NIOSSLTests/NIOSSLIntegrationTest+XCTest.swift b/Tests/NIOSSLTests/NIOSSLIntegrationTest+XCTest.swift index 583cea57..984dec74 100644 --- a/Tests/NIOSSLTests/NIOSSLIntegrationTest+XCTest.swift +++ b/Tests/NIOSSLTests/NIOSSLIntegrationTest+XCTest.swift @@ -52,6 +52,8 @@ extension NIOSSLIntegrationTest { ("testErroringNewVerificationCallback", testErroringNewVerificationCallback), ("testNewCallbackCanDelayHandshake", testNewCallbackCanDelayHandshake), ("testExtractingCertificatesNewCallback", testExtractingCertificatesNewCallback), + ("testNewCallbackCombinedWithDefaultTrustStore", testNewCallbackCombinedWithDefaultTrustStore), + ("testMacOSVerificationCallbackIsNotUsedIfVerificationDisabled", testMacOSVerificationCallbackIsNotUsedIfVerificationDisabled), ("testRepeatedClosure", testRepeatedClosure), ("testClosureTimeout", testClosureTimeout), ("testReceivingGibberishAfterAttemptingToClose", testReceivingGibberishAfterAttemptingToClose), diff --git a/Tests/NIOSSLTests/NIOSSLIntegrationTest.swift b/Tests/NIOSSLTests/NIOSSLIntegrationTest.swift index 6341a522..d7a1f7af 100644 --- a/Tests/NIOSSLTests/NIOSSLIntegrationTest.swift +++ b/Tests/NIOSSLTests/NIOSSLIntegrationTest.swift @@ -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()