TLSConfig.swift 3.0 KB

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