ServerBuilder+NIOSSL.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 Server {
  20. /// Returns a `Server` builder configured with TLS.
  21. @available(
  22. *, deprecated,
  23. message: "Use one of 'usingTLSBackedByNIOSSL(on:certificateChain:privateKey:)', 'usingTLSBackedByNetworkFramework(on:with:)' or 'usingTLS(with:on:)'"
  24. )
  25. public static func secure(
  26. group: EventLoopGroup,
  27. certificateChain: [NIOSSLCertificate],
  28. privateKey: NIOSSLPrivateKey
  29. ) -> Builder.Secure {
  30. return Server.usingTLSBackedByNIOSSL(
  31. on: group,
  32. certificateChain: certificateChain,
  33. privateKey: privateKey
  34. )
  35. }
  36. /// Returns a `Server` builder configured with the 'NIOSSL' TLS backend.
  37. ///
  38. /// This builder may use either a `MultiThreadedEventLoopGroup` or a `NIOTSEventLoopGroup` (or an
  39. /// `EventLoop` from either group).
  40. public static func usingTLSBackedByNIOSSL(
  41. on group: EventLoopGroup,
  42. certificateChain: [NIOSSLCertificate],
  43. privateKey: NIOSSLPrivateKey
  44. ) -> Builder.Secure {
  45. return Builder.Secure(
  46. group: group,
  47. tlsConfiguration: .makeServerConfigurationBackedByNIOSSL(
  48. certificateChain: certificateChain.map { .certificate($0) },
  49. privateKey: .privateKey(privateKey)
  50. )
  51. )
  52. }
  53. }
  54. extension Server.Builder.Secure {
  55. /// Sets the trust roots to use to validate certificates. This only needs to be provided if you
  56. /// intend to validate certificates. Defaults to the system provided trust store (`.default`) if
  57. /// not set.
  58. ///
  59. /// - Note: May only be used with the 'NIOSSL' TLS backend.
  60. @discardableResult
  61. public func withTLS(trustRoots: NIOSSLTrustRoots) -> Self {
  62. self.tls.updateNIOTrustRoots(to: trustRoots)
  63. return self
  64. }
  65. /// Sets whether certificates should be verified. Defaults to `.none` if not set.
  66. ///
  67. /// - Note: May only be used with the 'NIOSSL' TLS backend.
  68. @discardableResult
  69. public func withTLS(certificateVerification: CertificateVerification) -> Self {
  70. self.tls.updateNIOCertificateVerification(to: certificateVerification)
  71. return self
  72. }
  73. }
  74. #endif // canImport(NIOSSL)