TLSConfig.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. public enum TLSConfig: Sendable {
  17. /// The serialization format of the provided certificates and private keys.
  18. public struct SerializationFormat: Sendable, Equatable {
  19. package enum Wrapped {
  20. case pem
  21. case der
  22. }
  23. package let wrapped: Wrapped
  24. public static let pem = Self(wrapped: .pem)
  25. public static let der = Self(wrapped: .der)
  26. }
  27. /// A description of where a certificate is coming from: either a byte array or a file.
  28. /// The serialization format is specified by ``TLSConfig/SerializationFormat``.
  29. public struct CertificateSource: Sendable {
  30. package enum Wrapped {
  31. case file(path: String, format: SerializationFormat)
  32. case bytes(bytes: [UInt8], format: SerializationFormat)
  33. }
  34. package let wrapped: Wrapped
  35. /// The certificate's source is a file.
  36. /// - Parameters:
  37. /// - path: The file path containing the certificate.
  38. /// - format: The certificate's format, as a ``TLSConfig/SerializationFormat``.
  39. /// - Returns: A source describing the certificate source is the given file.
  40. public static func file(path: String, format: SerializationFormat) -> Self {
  41. Self(wrapped: .file(path: path, format: format))
  42. }
  43. /// The certificate's source is an array of bytes.
  44. /// - Parameters:
  45. /// - bytes: The array of bytes making up the certificate.
  46. /// - format: The certificate's format, as a ``TLSConfig/SerializationFormat``.
  47. /// - Returns: A source describing the certificate source is the given bytes.
  48. public static func bytes(_ bytes: [UInt8], format: SerializationFormat) -> Self {
  49. Self(wrapped: .bytes(bytes: bytes, format: format))
  50. }
  51. }
  52. /// A description of where the private key is coming from: either a byte array or a file.
  53. /// The serialization format is specified by ``TLSConfig/SerializationFormat``.
  54. public struct PrivateKeySource: Sendable {
  55. package enum Wrapped {
  56. case file(path: String, format: SerializationFormat)
  57. case bytes(bytes: [UInt8], format: SerializationFormat)
  58. }
  59. package let wrapped: Wrapped
  60. /// The private key's source is a file.
  61. /// - Parameters:
  62. /// - path: The file path containing the private key.
  63. /// - format: The private key's format, as a ``TLSConfig/SerializationFormat``.
  64. /// - Returns: A source describing the private key source is the given file.
  65. public static func file(path: String, format: SerializationFormat) -> Self {
  66. Self(wrapped: .file(path: path, format: format))
  67. }
  68. /// The private key's source is an array of bytes.
  69. /// - Parameters:
  70. /// - bytes: The array of bytes making up the private key.
  71. /// - format: The private key's format, as a ``TLSConfig/SerializationFormat``.
  72. /// - Returns: A source describing the private key source is the given bytes.
  73. public static func bytes(
  74. _ bytes: [UInt8],
  75. format: SerializationFormat
  76. ) -> Self {
  77. Self(wrapped: .bytes(bytes: bytes, format: format))
  78. }
  79. }
  80. /// A description of where the trust roots are coming from: either a custom certificate chain, or the system default trust store.
  81. public struct TrustRootsSource: Sendable {
  82. package enum Wrapped {
  83. case certificates([CertificateSource])
  84. case systemDefault
  85. }
  86. package let wrapped: Wrapped
  87. /// A list of ``TLSConfig/CertificateSource``s making up the
  88. /// chain of trust.
  89. /// - Parameter certificateSources: The sources for the certificates that make up the chain of trust.
  90. /// - Returns: A trust root for the given chain of trust.
  91. public static func certificates(
  92. _ certificateSources: [CertificateSource]
  93. ) -> Self {
  94. Self(wrapped: .certificates(certificateSources))
  95. }
  96. /// The system default trust store.
  97. public static let systemDefault: Self = Self(wrapped: .systemDefault)
  98. }
  99. /// How to verify client certificates.
  100. public struct CertificateVerification: Sendable {
  101. package enum Wrapped {
  102. case doNotVerify
  103. case fullVerification
  104. case noHostnameVerification
  105. }
  106. package let wrapped: Wrapped
  107. /// All certificate verification disabled.
  108. public static let noVerification: Self = Self(wrapped: .doNotVerify)
  109. /// Certificates will be validated against the trust store, but will not be checked to see if they are valid for the given hostname.
  110. public static let noHostnameVerification: Self = Self(wrapped: .noHostnameVerification)
  111. /// Certificates will be validated against the trust store and checked against the hostname of the service we are contacting.
  112. public static let fullVerification: Self = Self(wrapped: .fullVerification)
  113. }
  114. }
  115. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  116. extension HTTP2ServerTransport.Posix.Config {
  117. /// The security configuration for this connection.
  118. public struct TransportSecurity: Sendable {
  119. package enum Wrapped: Sendable {
  120. case plaintext
  121. case tls(TLS)
  122. }
  123. package let wrapped: Wrapped
  124. /// This connection is plaintext: no encryption will take place.
  125. public static let plaintext = Self(wrapped: .plaintext)
  126. /// This connection will use TLS.
  127. public static func tls(_ tls: TLS) -> Self {
  128. Self(wrapped: .tls(tls))
  129. }
  130. }
  131. public struct TLS: Sendable {
  132. /// The certificates the server will offer during negotiation.
  133. public var certificateChain: [TLSConfig.CertificateSource]
  134. /// The private key associated with the leaf certificate.
  135. public var privateKey: TLSConfig.PrivateKeySource
  136. /// How to verify the client certificate, if one is presented.
  137. public var clientCertificateVerification: TLSConfig.CertificateVerification
  138. /// The trust roots to be used when verifying client certificates.
  139. public var trustRoots: TLSConfig.TrustRootsSource
  140. /// Whether ALPN is required.
  141. ///
  142. /// If this is set to `true` but the client does not support ALPN, then the connection will be rejected.
  143. public var requireALPN: Bool
  144. /// Create a new HTTP2 NIO Posix transport TLS config, with some values defaulted:
  145. /// - `clientCertificateVerificationMode` equals `doNotVerify`
  146. /// - `trustRoots` equals `systemDefault`
  147. /// - `requireALPN` equals `false`
  148. ///
  149. /// - Parameters:
  150. /// - certificateChain: The certificates the server will offer during negotiation.
  151. /// - privateKey: The private key associated with the leaf certificate.
  152. /// - Returns: A new HTTP2 NIO Posix transport TLS config.
  153. public static func defaults(
  154. certificateChain: [TLSConfig.CertificateSource],
  155. privateKey: TLSConfig.PrivateKeySource
  156. ) -> Self {
  157. Self.init(
  158. certificateChain: certificateChain,
  159. privateKey: privateKey,
  160. clientCertificateVerification: .noVerification,
  161. trustRoots: .systemDefault,
  162. requireALPN: false
  163. )
  164. }
  165. /// Create a new HTTP2 NIO Posix transport TLS config, with some values defaulted to match
  166. /// the requirements of mTLS:
  167. /// - `clientCertificateVerificationMode` equals `noHostnameVerification`
  168. /// - `trustRoots` equals `systemDefault`
  169. /// - `requireALPN` equals `false`
  170. ///
  171. /// - Parameters:
  172. /// - certificateChain: The certificates the server will offer during negotiation.
  173. /// - privateKey: The private key associated with the leaf certificate.
  174. /// - Returns: A new HTTP2 NIO Posix transport TLS config.
  175. public static func mTLS(
  176. certificateChain: [TLSConfig.CertificateSource],
  177. privateKey: TLSConfig.PrivateKeySource
  178. ) -> Self {
  179. Self.init(
  180. certificateChain: certificateChain,
  181. privateKey: privateKey,
  182. clientCertificateVerification: .noHostnameVerification,
  183. trustRoots: .systemDefault,
  184. requireALPN: false
  185. )
  186. }
  187. }
  188. }