TLSVerificationHandler.swift 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import Foundation
  2. import NIO
  3. import NIOSSL
  4. import NIOTLS
  5. /// Application protocol identifiers for ALPN.
  6. public enum GRPCApplicationProtocolIdentifier: String, CaseIterable {
  7. // This is not in the IANA ALPN protocol ID registry, but may be used by servers to indicate that
  8. // they serve only gRPC traffic. It is part of the gRPC core implementation.
  9. case gRPC = "grpc-ext"
  10. case h2 = "h2"
  11. }
  12. /// A helper `ChannelInboundHandler` to verify that a TLS handshake was completed successfully
  13. /// and that the negotiated application protocol is valid.
  14. ///
  15. /// The handler holds a promise which is succeeded on successful verification of the negotiated
  16. /// application protocol and failed if any error is received by this handler or an invalid
  17. /// application protocol was negotiated.
  18. ///
  19. /// Users of this handler should rely on the `verification` future held by this instance.
  20. ///
  21. /// On fulfillment of the promise this handler is removed from the channel pipeline.
  22. public class TLSVerificationHandler: ChannelInboundHandler, RemovableChannelHandler {
  23. public typealias InboundIn = Any
  24. private var verificationPromise: EventLoopPromise<Void>!
  25. private let delegate: ClientErrorDelegate?
  26. /// A future which is fulfilled when the state of the TLS handshake is known. If the handshake
  27. /// was successful and the negotiated application protocol is valid then the future is succeeded.
  28. /// If an error occured or the application protocol is not valid then the future will have been
  29. /// failed.
  30. ///
  31. /// - Important: The promise associated with this future is created in `handlerAdded(context:)`,
  32. /// and as such must _not_ be accessed before the handler has be added to a pipeline.
  33. public var verification: EventLoopFuture<Void>! {
  34. return verificationPromise.futureResult
  35. }
  36. public init(errorDelegate: ClientErrorDelegate?) {
  37. self.delegate = errorDelegate
  38. }
  39. public func handlerAdded(context: ChannelHandlerContext) {
  40. self.verificationPromise = context.eventLoop.makePromise()
  41. // Remove ourselves from the pipeline when the promise gets fulfilled.
  42. self.verificationPromise.futureResult.whenComplete { _ in
  43. context.pipeline.removeHandler(self, promise: nil)
  44. }
  45. }
  46. public func errorCaught(context: ChannelHandlerContext, error: Error) {
  47. precondition(self.verificationPromise != nil, "handler has not been added to the pipeline")
  48. if let delegate = self.delegate {
  49. let grpcError = (error as? GRPCError) ?? GRPCError.unknown(error, origin: .client)
  50. delegate.didCatchError(grpcError.wrappedError, file: grpcError.file, line: grpcError.line)
  51. }
  52. verificationPromise.fail(error)
  53. }
  54. public func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
  55. precondition(self.verificationPromise != nil, "handler has not been added to the pipeline")
  56. guard let tlsEvent = event as? TLSUserEvent,
  57. case .handshakeCompleted(negotiatedProtocol: let negotiatedProtocol) = tlsEvent else {
  58. context.fireUserInboundEventTriggered(event)
  59. return
  60. }
  61. if let proto = negotiatedProtocol, GRPCApplicationProtocolIdentifier(rawValue: proto) != nil {
  62. self.verificationPromise.succeed(())
  63. } else {
  64. self.verificationPromise.fail(GRPCError.client(.applicationLevelProtocolNegotiationFailed))
  65. }
  66. }
  67. }