ServerBuilder+NIOSSL.swift 2.7 KB

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