ClientConnection+NIOSSL.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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(NIOSSL)
  17. import NIOCore
  18. import NIOSSL
  19. extension ClientConnection {
  20. /// Returns a `ClientConnection` builder configured with TLS.
  21. @available(
  22. *, deprecated,
  23. message: "Use one of 'usingPlatformAppropriateTLS(for:)', 'usingTLSBackedByNIOSSL(on:)' or 'usingTLSBackedByNetworkFramework(on:)' or 'usingTLS(on:with:)'"
  24. )
  25. public static func secure(group: EventLoopGroup) -> ClientConnection.Builder.Secure {
  26. return ClientConnection.usingTLSBackedByNIOSSL(on: group)
  27. }
  28. /// Returns a `ClientConnection` builder configured with the 'NIOSSL' TLS backend.
  29. ///
  30. /// This builder may use either a `MultiThreadedEventLoopGroup` or a `NIOTSEventLoopGroup` (or an
  31. /// `EventLoop` from either group).
  32. ///
  33. /// - Parameter group: The `EventLoopGroup` use for the connection.
  34. /// - Returns: A builder for a connection using the NIOSSL TLS backend.
  35. public static func usingTLSBackedByNIOSSL(
  36. on group: EventLoopGroup
  37. ) -> ClientConnection.Builder.Secure {
  38. return Builder.Secure(group: group, tlsConfiguration: .makeClientConfigurationBackedByNIOSSL())
  39. }
  40. }
  41. // MARK: - NIOSSL TLS backend options
  42. extension ClientConnection.Builder.Secure {
  43. /// Sets the sources of certificates to offer during negotiation. No certificates are offered
  44. /// during negotiation by default.
  45. ///
  46. /// - Note: May only be used with the 'NIOSSL' TLS backend.
  47. @discardableResult
  48. public func withTLS(certificateChain: [NIOSSLCertificate]) -> Self {
  49. self.tls.updateNIOCertificateChain(to: certificateChain)
  50. return self
  51. }
  52. /// Sets the private key associated with the leaf certificate.
  53. ///
  54. /// - Note: May only be used with the 'NIOSSL' TLS backend.
  55. @discardableResult
  56. public func withTLS(privateKey: NIOSSLPrivateKey) -> Self {
  57. self.tls.updateNIOPrivateKey(to: privateKey)
  58. return self
  59. }
  60. /// Sets the trust roots to use to validate certificates. This only needs to be provided if you
  61. /// intend to validate certificates. Defaults to the system provided trust store (`.default`) if
  62. /// not set.
  63. ///
  64. /// - Note: May only be used with the 'NIOSSL' TLS backend.
  65. @discardableResult
  66. public func withTLS(trustRoots: NIOSSLTrustRoots) -> Self {
  67. self.tls.updateNIOTrustRoots(to: trustRoots)
  68. return self
  69. }
  70. /// Whether to verify remote certificates. Defaults to `.fullVerification` if not otherwise
  71. /// configured.
  72. ///
  73. /// - Note: May only be used with the 'NIOSSL' TLS backend.
  74. @discardableResult
  75. public func withTLS(certificateVerification: CertificateVerification) -> Self {
  76. self.tls.updateNIOCertificateVerification(to: certificateVerification)
  77. return self
  78. }
  79. /// A custom verification callback that allows completely overriding the certificate verification logic.
  80. ///
  81. /// - Note: May only be used with the 'NIOSSL' TLS backend.
  82. @discardableResult
  83. public func withTLSCustomVerificationCallback(
  84. _ callback: @escaping NIOSSLCustomVerificationCallback
  85. ) -> Self {
  86. self.tls.updateNIOCustomVerificationCallback(to: callback)
  87. return self
  88. }
  89. }
  90. #endif // canImport(NIOSSL)