ClientConnection+NIOSSL.swift 3.6 KB

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