TLSConfig.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2024, 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(Network)
  17. public import Network
  18. extension HTTP2ServerTransport.TransportServices.Config {
  19. /// The security configuration for this connection.
  20. public struct TransportSecurity: Sendable {
  21. package enum Wrapped: Sendable {
  22. case plaintext
  23. case tls(TLS)
  24. }
  25. package let wrapped: Wrapped
  26. /// This connection is plaintext: no encryption will take place.
  27. public static let plaintext = Self(wrapped: .plaintext)
  28. /// This connection will use TLS.
  29. public static func tls(_ tls: TLS) -> Self {
  30. Self(wrapped: .tls(tls))
  31. }
  32. }
  33. public struct TLS: Sendable {
  34. /// A provider for the `SecIdentity` to be used when setting up TLS.
  35. public var identityProvider: @Sendable () throws -> SecIdentity
  36. /// Whether ALPN is required.
  37. ///
  38. /// If this is set to `true` but the client does not support ALPN, then the connection will be rejected.
  39. public var requireALPN: Bool
  40. /// Create a new HTTP2 NIO Transport Services transport TLS config.
  41. /// - Parameters:
  42. /// - requireALPN: Whether ALPN is required.
  43. /// - identityProvider: A provider for the `SecIdentity` to be used when setting up TLS.
  44. public init(
  45. requireALPN: Bool,
  46. identityProvider: @Sendable @escaping () throws -> SecIdentity
  47. ) {
  48. self.requireALPN = requireALPN
  49. self.identityProvider = identityProvider
  50. }
  51. /// Create a new HTTP2 NIO Transport Services transport TLS config, with some values defaulted:
  52. /// - `requireALPN` equals `false`
  53. ///
  54. /// - Returns: A new HTTP2 NIO Transport Services transport TLS config.
  55. public static func defaults(
  56. identityProvider: @Sendable @escaping () throws -> SecIdentity
  57. ) -> Self {
  58. Self(requireALPN: false, identityProvider: identityProvider)
  59. }
  60. }
  61. }
  62. extension HTTP2ClientTransport.TransportServices.Config {
  63. /// The security configuration for this connection.
  64. public struct TransportSecurity: Sendable {
  65. package enum Wrapped: Sendable {
  66. case plaintext
  67. case tls(TLS)
  68. }
  69. package let wrapped: Wrapped
  70. /// This connection is plaintext: no encryption will take place.
  71. public static let plaintext = Self(wrapped: .plaintext)
  72. /// This connection will use TLS.
  73. public static func tls(_ tls: TLS) -> Self {
  74. Self(wrapped: .tls(tls))
  75. }
  76. }
  77. public struct TLS: Sendable {
  78. /// A provider for the `SecIdentity` to be used when setting up TLS.
  79. public var identityProvider: @Sendable () throws -> SecIdentity
  80. /// Create a new HTTP2 NIO Transport Services transport TLS config.
  81. public init(identityProvider: @Sendable @escaping () throws -> SecIdentity) {
  82. self.identityProvider = identityProvider
  83. }
  84. }
  85. }
  86. #endif