2
0

GRPCTLSVerificationHandler.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 GRPCTLSVerificationHandler: ChannelInboundHandler, RemovableChannelHandler {
  23. public typealias InboundIn = Any
  24. private var verificationPromise: EventLoopPromise<Void>!
  25. /// A future which is fulfilled when the state of the TLS handshake is known. If the handshake
  26. /// was successful and the negotiated application protocol is valid then the future is succeeded.
  27. /// If an error occured or the application protocol is not valid then the future will have been
  28. /// failed.
  29. ///
  30. /// - Important: The promise associated with this future is created in `handlerAdded(context:)`,
  31. /// and as such must _not_ be accessed before the handler has be added to a pipeline.
  32. public var verification: EventLoopFuture<Void>! {
  33. return verificationPromise.futureResult
  34. }
  35. public init() { }
  36. public func handlerAdded(context: ChannelHandlerContext) {
  37. self.verificationPromise = context.eventLoop.makePromise()
  38. // Remove ourselves from the pipeline when the promise gets fulfilled.
  39. self.verificationPromise.futureResult.whenComplete { _ in
  40. context.pipeline.removeHandler(self, promise: nil)
  41. }
  42. }
  43. public func errorCaught(context: ChannelHandlerContext, error: Error) {
  44. precondition(self.verificationPromise != nil, "handler has not been added to the pipeline")
  45. verificationPromise.fail(error)
  46. }
  47. public func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
  48. precondition(self.verificationPromise != nil, "handler has not been added to the pipeline")
  49. guard let tlsEvent = event as? TLSUserEvent,
  50. case .handshakeCompleted(negotiatedProtocol: let negotiatedProtocol) = tlsEvent else {
  51. context.fireUserInboundEventTriggered(event)
  52. return
  53. }
  54. if let proto = negotiatedProtocol, GRPCApplicationProtocolIdentifier(rawValue: proto) != nil {
  55. self.verificationPromise.succeed(())
  56. } else {
  57. self.verificationPromise.fail(GRPCError.client(.applicationLevelProtocolNegotiationFailed))
  58. }
  59. }
  60. }