Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom verify callback to servers. #226

Merged
merged 1 commit into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Sources/NIOSSL/NIOSSLServerHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import NIO
/// handler can be used in channels that are acting as the server in
/// the TLS dialog. For client connections, use the `NIOSSLClientHandler`.
public final class NIOSSLServerHandler: NIOSSLHandler {
public convenience init(context: NIOSSLContext) {
self.init(context: context, optionalCustomVerificationCallback: nil)
}

@available(*, deprecated, renamed: "init(context:serverHostname:customVerificationCallback:)")
public init(context: NIOSSLContext, verificationCallback: NIOSSLVerificationCallback? = nil) throws {
guard let connection = context.createConnection() else {
fatalError("Failed to create new connection in NIOSSLContext")
Expand All @@ -31,4 +36,23 @@ public final class NIOSSLServerHandler: NIOSSLHandler {

super.init(connection: connection, shutdownTimeout: context.configuration.shutdownTimeout)
}

public convenience init(context: NIOSSLContext, customVerificationCallback: @escaping NIOSSLCustomVerificationCallback) {
self.init(context: context, optionalCustomVerificationCallback: customVerificationCallback)
}

/// This exists to handle the explosion of initializers I got when I deprecated the first one.
private init(context: NIOSSLContext, optionalCustomVerificationCallback: NIOSSLCustomVerificationCallback?) {
guard let connection = context.createConnection() else {
fatalError("Failed to create new connection in NIOSSLContext")
}

connection.setAcceptState()

if let customVerificationCallback = optionalCustomVerificationCallback {
connection.setCustomVerificationCallback(.init(callback: customVerificationCallback))
}

super.init(connection: connection, shutdownTimeout: context.configuration.shutdownTimeout)
}
}
2 changes: 1 addition & 1 deletion Sources/NIOSSLPerformanceTester/BenchManyWrites.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class BenchManyWrites: Benchmark {
}

func setUp() throws {
let serverHandler = try NIOSSLServerHandler(context: self.serverContext)
let serverHandler = NIOSSLServerHandler(context: self.serverContext)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥳

let clientHandler = try NIOSSLClientHandler(context: self.clientContext, serverHostname: "localhost")
try self.backToBack.client.pipeline.addHandler(clientHandler).wait()
try self.backToBack.server.pipeline.addHandler(serverHandler).wait()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class BenchRepeatedHandshakes: Benchmark {
func run() throws -> Int {
for _ in 0..<self.loopCount {
let backToBack = BackToBackEmbeddedChannel()
let serverHandler = try NIOSSLServerHandler(context: self.serverContext)
let serverHandler = NIOSSLServerHandler(context: self.serverContext)
let clientHandler = try NIOSSLClientHandler(context: self.clientContext, serverHostname: "localhost")
try backToBack.client.pipeline.addHandler(clientHandler).wait()
try backToBack.server.pipeline.addHandler(serverHandler).wait()
Expand Down
2 changes: 1 addition & 1 deletion Sources/NIOTLSServer/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ let bootstrap = ServerBootstrap(group: group)

// Set the handlers that are applied to the accepted channels.
.childChannelInitializer { channel in
return channel.pipeline.addHandler(try! NIOSSLServerHandler(context: sslContext)).flatMap {
return channel.pipeline.addHandler(NIOSSLServerHandler(context: sslContext)).flatMap {
channel.pipeline.addHandler(EchoHandler())
}
}
Expand Down
1 change: 1 addition & 0 deletions Tests/NIOSSLTests/NIOSSLIntegrationTest+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ extension NIOSSLIntegrationTest {
("testExtractingCertificatesNewCallback", testExtractingCertificatesNewCallback),
("testNewCallbackCombinedWithDefaultTrustStore", testNewCallbackCombinedWithDefaultTrustStore),
("testMacOSVerificationCallbackIsNotUsedIfVerificationDisabled", testMacOSVerificationCallbackIsNotUsedIfVerificationDisabled),
("testServerHasNewCallbackCalledToo", testServerHasNewCallbackCalledToo),
("testRepeatedClosure", testRepeatedClosure),
("testClosureTimeout", testClosureTimeout),
("testReceivingGibberishAfterAttemptingToClose", testReceivingGibberishAfterAttemptingToClose),
Expand Down
73 changes: 59 additions & 14 deletions Tests/NIOSSLTests/NIOSSLIntegrationTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,19 +270,24 @@ internal func serverTLSChannel(context: NIOSSLContext,
preHandlers: [ChannelHandler],
postHandlers: [ChannelHandler],
group: EventLoopGroup,
customVerificationCallback: NIOSSLCustomVerificationCallback? = nil,
file: StaticString = #file,
line: UInt = #line) throws -> Channel {
return try assertNoThrowWithValue(ServerBootstrap(group: group)
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
.childChannelInitializer { channel in
let results = preHandlers.map { channel.pipeline.addHandler($0) }
return EventLoopFuture<Void>.andAllSucceed(results, on: channel.eventLoop).flatMapThrowing {
try NIOSSLServerHandler(context: context)
}.flatMap {
channel.pipeline.addHandler($0)
}.flatMap {
let results = postHandlers.map { channel.pipeline.addHandler($0) }
return EventLoopFuture<Void>.andAllSucceed(results, on: channel.eventLoop)
return EventLoopFuture<Void>.andAllSucceed(results, on: channel.eventLoop).map {
if let verify = customVerificationCallback {
return NIOSSLServerHandler(context: context, customVerificationCallback: verify)
} else {
return NIOSSLServerHandler(context: context)
}
}.flatMap {
channel.pipeline.addHandler($0)
}.flatMap {
let results = postHandlers.map { channel.pipeline.addHandler($0) }
return EventLoopFuture<Void>.andAllSucceed(results, on: channel.eventLoop)
}
}.bind(host: "127.0.0.1", port: 0).wait(), file: file, line: line)
}
Expand Down Expand Up @@ -902,7 +907,7 @@ class NIOSSLIntegrationTest: XCTestCase {

let context = try configuredSSLContext()

try serverChannel.pipeline.addHandler(try NIOSSLServerHandler(context: context)).wait()
try serverChannel.pipeline.addHandler(NIOSSLServerHandler(context: context)).wait()
try clientChannel.pipeline.addHandler(try NIOSSLClientHandler(context: context, serverHostname: nil)).wait()

let addr: SocketAddress = try SocketAddress(unixDomainSocketPath: "/tmp/whatever")
Expand Down Expand Up @@ -1039,7 +1044,7 @@ class NIOSSLIntegrationTest: XCTestCase {

let context = try configuredSSLContext()

try serverChannel.pipeline.addHandler(try NIOSSLServerHandler(context: context)).wait()
try serverChannel.pipeline.addHandler(NIOSSLServerHandler(context: context)).wait()
try clientChannel.pipeline.addHandler(try NIOSSLClientHandler(context: context, serverHostname: nil)).wait()

let addr = try SocketAddress(unixDomainSocketPath: "/tmp/whatever2")
Expand Down Expand Up @@ -1076,7 +1081,7 @@ class NIOSSLIntegrationTest: XCTestCase {

let completePromise: EventLoopPromise<ByteBuffer> = serverChannel.eventLoop.makePromise()

XCTAssertNoThrow(try serverChannel.pipeline.addHandler(try NIOSSLServerHandler(context: context)).wait())
XCTAssertNoThrow(try serverChannel.pipeline.addHandler(NIOSSLServerHandler(context: context)).wait())
XCTAssertNoThrow(try serverChannel.pipeline.addHandler(ReadRecordingHandler(completePromise: completePromise)).wait())
XCTAssertNoThrow(try clientChannel.pipeline.addHandler(try NIOSSLClientHandler(context: context, serverHostname: nil)).wait())

Expand Down Expand Up @@ -1158,7 +1163,7 @@ class NIOSSLIntegrationTest: XCTestCase {

let context = try configuredSSLContext()

try serverChannel.pipeline.addHandler(try NIOSSLServerHandler(context: context)).wait()
try serverChannel.pipeline.addHandler(NIOSSLServerHandler(context: context)).wait()
try clientChannel.pipeline.addHandler(try NIOSSLClientHandler(context: context, serverHostname: nil)).wait()

let addr = try SocketAddress(unixDomainSocketPath: "/tmp/whatever2")
Expand Down Expand Up @@ -1244,7 +1249,7 @@ class NIOSSLIntegrationTest: XCTestCase {

let completePromise: EventLoopPromise<ByteBuffer> = serverChannel.eventLoop.makePromise()

XCTAssertNoThrow(try serverChannel.pipeline.addHandler(try NIOSSLServerHandler(context: context)).wait())
XCTAssertNoThrow(try serverChannel.pipeline.addHandler(NIOSSLServerHandler(context: context)).wait())
XCTAssertNoThrow(try serverChannel.pipeline.addHandler(ReadRecordingHandler(completePromise: completePromise)).wait())
XCTAssertNoThrow(try clientChannel.pipeline.addHandler(try NIOSSLClientHandler(context: context, serverHostname: nil)).wait())

Expand Down Expand Up @@ -1607,6 +1612,46 @@ class NIOSSLIntegrationTest: XCTestCase {
XCTAssertNoThrow(try handshakeCompletePromise.futureResult.wait())
}

func testServerHasNewCallbackCalledToo() throws {
let config = TLSConfiguration.forServer(certificateChain: [.certificate(NIOSSLIntegrationTest.cert)],
privateKey: .privateKey(NIOSSLIntegrationTest.key),
certificateVerification: .fullVerification,
trustRoots: .default)
let context = try assertNoThrowWithValue(NIOSSLContext(configuration: config))

let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
XCTAssertNoThrow(try group.syncShutdownGracefully())
}

let handshakeResultPromise = group.next().makePromise(of: Void.self)
let handshakeWatcher = WaitForHandshakeHandler(handshakeResultPromise: handshakeResultPromise)
let serverChannel: Channel = try serverTLSChannel(context: context,
preHandlers: [],
postHandlers: [handshakeWatcher],
group: group,
customVerificationCallback: { _, promise in
promise.succeed(.failed)
})
Lukasa marked this conversation as resolved.
Show resolved Hide resolved
defer {
XCTAssertNoThrow(try serverChannel.close().wait())
}


let clientChannel = try clientTLSChannel(context: try configuredSSLContext(),
preHandlers: [],
postHandlers: [],
group: group,
connectingTo: serverChannel.localAddress!)

defer {
// Ignore errors here, the channel should be closed already by the time this happens.
try? clientChannel.close().wait()
}

XCTAssertThrowsError(try handshakeResultPromise.futureResult.wait())
}

func testRepeatedClosure() throws {
let serverChannel = EmbeddedChannel()
let clientChannel = EmbeddedChannel()
Expand Down Expand Up @@ -1834,7 +1879,7 @@ class NIOSSLIntegrationTest: XCTestCase {
}

XCTAssertNoThrow(try serverChannel.pipeline.addHandler(SecondChannelInactiveSwallower()).wait())
XCTAssertNoThrow(try serverChannel.pipeline.addHandler(try NIOSSLServerHandler(context: context)).wait())
XCTAssertNoThrow(try serverChannel.pipeline.addHandler(NIOSSLServerHandler(context: context)).wait())
XCTAssertNoThrow(try serverChannel.pipeline.addHandler(FlushOnReadHandler()).wait())

XCTAssertNoThrow(try clientChannel.pipeline.addHandler(try NIOSSLClientHandler(context: context, serverHostname: nil)).wait())
Expand Down Expand Up @@ -1955,7 +2000,7 @@ class NIOSSLIntegrationTest: XCTestCase {

let context = try configuredSSLContext()

try serverChannel.pipeline.addHandler(try NIOSSLServerHandler(context: context)).wait()
try serverChannel.pipeline.addHandler(NIOSSLServerHandler(context: context)).wait()
try clientChannel.pipeline.addHandler(try NIOSSLClientHandler(context: context, serverHostname: nil)).wait()

// Do the handshake.
Expand Down