ClientConnection+NWTLS.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright 2021, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #if canImport(Security)
  17. #if canImport(Network)
  18. import NIOCore
  19. import Security
  20. extension ClientConnection {
  21. /// Returns a ``ClientConnection`` builder configured with the Network.framework TLS backend.
  22. ///
  23. /// This builder must use a `NIOTSEventLoopGroup` (or an `EventLoop` from a
  24. /// `NIOTSEventLoopGroup`).
  25. ///
  26. /// - Parameter group: The `EventLoopGroup` use for the connection.
  27. /// - Returns: A builder for a connection using the Network.framework TLS backend.
  28. @available(macOS 10.14, iOS 12.0, watchOS 6.0, tvOS 12.0, *)
  29. public static func usingTLSBackedByNetworkFramework(
  30. on group: EventLoopGroup
  31. ) -> ClientConnection.Builder.Secure {
  32. precondition(
  33. PlatformSupport.isTransportServicesEventLoopGroup(group),
  34. "'\(#function)' requires 'group' to be a 'NIOTransportServices.NIOTSEventLoopGroup' or 'NIOTransportServices.QoSEventLoop' (but was '\(type(of: group))'"
  35. )
  36. return Builder.Secure(
  37. group: group,
  38. tlsConfiguration: .makeClientConfigurationBackedByNetworkFramework()
  39. )
  40. }
  41. }
  42. extension ClientConnection.Builder.Secure {
  43. /// Update the local identity.
  44. ///
  45. /// - Note: May only be used with the 'Network.framework' TLS backend.
  46. @discardableResult
  47. @available(macOS 10.14, iOS 12.0, watchOS 6.0, tvOS 12.0, *)
  48. public func withTLS(localIdentity: SecIdentity) -> Self {
  49. self.tls.updateNetworkLocalIdentity(to: localIdentity)
  50. return self
  51. }
  52. /// Update the callback used to verify a trust object during a TLS handshake.
  53. ///
  54. /// - Note: May only be used with the 'Network.framework' TLS backend.
  55. @discardableResult
  56. @available(macOS 10.14, iOS 12.0, watchOS 6.0, tvOS 12.0, *)
  57. public func withTLSHandshakeVerificationCallback(
  58. on queue: DispatchQueue,
  59. verificationCallback callback: @escaping sec_protocol_verify_t
  60. ) -> Self {
  61. self.tls.updateNetworkVerifyCallbackWithQueue(callback: callback, queue: queue)
  62. return self
  63. }
  64. }
  65. #endif // canImport(Network)
  66. #endif // canImport(Security)