Config+TLS.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. extension HTTP2ServerTransport.Posix.Config {
  17. /// The security configuration for this connection.
  18. public struct TransportSecurity: Sendable {
  19. package enum Wrapped: Sendable {
  20. case plaintext
  21. case tls(TLS)
  22. }
  23. package let wrapped: Wrapped
  24. /// This connection is plaintext: no encryption will take place.
  25. public static let plaintext = Self(wrapped: .plaintext)
  26. /// This connection will use TLS.
  27. public static func tls(_ tls: TLS) -> Self {
  28. Self(wrapped: .tls(tls))
  29. }
  30. }
  31. public struct TLS: Sendable {
  32. /// The certificates the server will offer during negotiation.
  33. public var certificateChain: [TLSConfig.CertificateSource]
  34. /// The private key associated with the leaf certificate.
  35. public var privateKey: TLSConfig.PrivateKeySource
  36. /// How to verify the client certificate, if one is presented.
  37. public var clientCertificateVerification: TLSConfig.CertificateVerification
  38. /// The trust roots to be used when verifying client certificates.
  39. public var trustRoots: TLSConfig.TrustRootsSource
  40. /// Whether ALPN is required.
  41. ///
  42. /// If this is set to `true` but the client does not support ALPN, then the connection will be rejected.
  43. public var requireALPN: Bool
  44. /// Create a new HTTP2 NIO Posix server transport TLS config.
  45. /// - Parameters:
  46. /// - certificateChain: The certificates the server will offer during negotiation.
  47. /// - privateKey: The private key associated with the leaf certificate.
  48. /// - clientCertificateVerification: How to verify the client certificate, if one is presented.
  49. /// - trustRoots: The trust roots to be used when verifying client certificates.
  50. /// - requireALPN: Whether ALPN is required.
  51. public init(
  52. certificateChain: [TLSConfig.CertificateSource],
  53. privateKey: TLSConfig.PrivateKeySource,
  54. clientCertificateVerification: TLSConfig.CertificateVerification,
  55. trustRoots: TLSConfig.TrustRootsSource,
  56. requireALPN: Bool
  57. ) {
  58. self.certificateChain = certificateChain
  59. self.privateKey = privateKey
  60. self.clientCertificateVerification = clientCertificateVerification
  61. self.trustRoots = trustRoots
  62. self.requireALPN = requireALPN
  63. }
  64. /// Create a new HTTP2 NIO Posix transport TLS config, with some values defaulted:
  65. /// - `clientCertificateVerificationMode` equals `doNotVerify`
  66. /// - `trustRoots` equals `systemDefault`
  67. /// - `requireALPN` equals `false`
  68. ///
  69. /// - Parameters:
  70. /// - certificateChain: The certificates the server will offer during negotiation.
  71. /// - privateKey: The private key associated with the leaf certificate.
  72. /// - configure: A closure which allows you to modify the defaults before returning them.
  73. /// - Returns: A new HTTP2 NIO Posix transport TLS config.
  74. public static func defaults(
  75. certificateChain: [TLSConfig.CertificateSource],
  76. privateKey: TLSConfig.PrivateKeySource,
  77. configure: (_ config: inout Self) -> Void = { _ in }
  78. ) -> Self {
  79. var config = Self(
  80. certificateChain: certificateChain,
  81. privateKey: privateKey,
  82. clientCertificateVerification: .noVerification,
  83. trustRoots: .systemDefault,
  84. requireALPN: false
  85. )
  86. configure(&config)
  87. return config
  88. }
  89. /// Create a new HTTP2 NIO Posix transport TLS config, with some values defaulted to match
  90. /// the requirements of mTLS:
  91. /// - `clientCertificateVerificationMode` equals `noHostnameVerification`
  92. /// - `trustRoots` equals `systemDefault`
  93. /// - `requireALPN` equals `false`
  94. ///
  95. /// - Parameters:
  96. /// - certificateChain: The certificates the server will offer during negotiation.
  97. /// - privateKey: The private key associated with the leaf certificate.
  98. /// - configure: A closure which allows you to modify the defaults before returning them.
  99. /// - Returns: A new HTTP2 NIO Posix transport TLS config.
  100. public static func mTLS(
  101. certificateChain: [TLSConfig.CertificateSource],
  102. privateKey: TLSConfig.PrivateKeySource,
  103. configure: (_ config: inout Self) -> Void = { _ in }
  104. ) -> Self {
  105. var config = Self(
  106. certificateChain: certificateChain,
  107. privateKey: privateKey,
  108. clientCertificateVerification: .noHostnameVerification,
  109. trustRoots: .systemDefault,
  110. requireALPN: false
  111. )
  112. configure(&config)
  113. return config
  114. }
  115. }
  116. }
  117. extension HTTP2ClientTransport.Posix.Config {
  118. /// The security configuration for this connection.
  119. public struct TransportSecurity: Sendable {
  120. package enum Wrapped: Sendable {
  121. case plaintext
  122. case tls(TLS)
  123. }
  124. package let wrapped: Wrapped
  125. /// This connection is plaintext: no encryption will take place.
  126. public static let plaintext = Self(wrapped: .plaintext)
  127. /// This connection will use TLS.
  128. public static func tls(_ tls: TLS) -> Self {
  129. Self(wrapped: .tls(tls))
  130. }
  131. }
  132. public struct TLS: Sendable {
  133. /// The certificates the client will offer during negotiation.
  134. public var certificateChain: [TLSConfig.CertificateSource]
  135. /// The private key associated with the leaf certificate.
  136. public var privateKey: TLSConfig.PrivateKeySource?
  137. /// How to verify the server certificate, if one is presented.
  138. public var serverCertificateVerification: TLSConfig.CertificateVerification
  139. /// The trust roots to be used when verifying server certificates.
  140. public var trustRoots: TLSConfig.TrustRootsSource
  141. /// An optional server hostname to use when verifying certificates.
  142. public var serverHostname: String?
  143. /// Create a new HTTP2 NIO Posix client transport TLS config.
  144. /// - Parameters:
  145. /// - certificateChain: The certificates the client will offer during negotiation.
  146. /// - privateKey: The private key associated with the leaf certificate.
  147. /// - serverCertificateVerification: How to verify the server certificate, if one is presented.
  148. /// - trustRoots: The trust roots to be used when verifying server certificates.
  149. /// - serverHostname: An optional server hostname to use when verifying certificates.
  150. public init(
  151. certificateChain: [TLSConfig.CertificateSource],
  152. privateKey: TLSConfig.PrivateKeySource?,
  153. serverCertificateVerification: TLSConfig.CertificateVerification,
  154. trustRoots: TLSConfig.TrustRootsSource,
  155. serverHostname: String?
  156. ) {
  157. self.certificateChain = certificateChain
  158. self.privateKey = privateKey
  159. self.serverCertificateVerification = serverCertificateVerification
  160. self.trustRoots = trustRoots
  161. self.serverHostname = serverHostname
  162. }
  163. /// Create a new HTTP2 NIO Posix transport TLS config, with some values defaulted:
  164. /// - `certificateChain` equals `[]`
  165. /// - `privateKey` equals `nil`
  166. /// - `serverCertificateVerification` equals `fullVerification`
  167. /// - `trustRoots` equals `systemDefault`
  168. /// - `serverHostname` equals `nil`
  169. ///
  170. /// - Parameters:
  171. /// - configure: A closure which allows you to modify the defaults before returning them.
  172. /// - Returns: A new HTTP2 NIO Posix transport TLS config.
  173. public static func defaults(
  174. configure: (_ config: inout Self) -> Void = { _ in }
  175. ) -> Self {
  176. var config = Self(
  177. certificateChain: [],
  178. privateKey: nil,
  179. serverCertificateVerification: .fullVerification,
  180. trustRoots: .systemDefault,
  181. serverHostname: nil
  182. )
  183. configure(&config)
  184. return config
  185. }
  186. /// Create a new HTTP2 NIO Posix transport TLS config, with some values defaulted:
  187. /// - `certificateChain` equals `[]`
  188. /// - `privateKey` equals `nil`
  189. /// - `serverCertificateVerification` equals `fullVerification`
  190. /// - `trustRoots` equals `systemDefault`
  191. /// - `serverHostname` equals `nil`
  192. public static var defaults: Self { .defaults() }
  193. /// Create a new HTTP2 NIO Posix transport TLS config, with some values defaulted to match
  194. /// the requirements of mTLS:
  195. /// - `trustRoots` equals `systemDefault`
  196. /// - `serverCertificateVerification` equals `fullVerification`
  197. /// - `serverHostname` equals `nil`
  198. ///
  199. /// - Parameters:
  200. /// - certificateChain: The certificates the client will offer during negotiation.
  201. /// - privateKey: The private key associated with the leaf certificate.
  202. /// - configure: A closure which allows you to modify the defaults before returning them.
  203. /// - Returns: A new HTTP2 NIO Posix transport TLS config.
  204. public static func mTLS(
  205. certificateChain: [TLSConfig.CertificateSource],
  206. privateKey: TLSConfig.PrivateKeySource,
  207. configure: (_ config: inout Self) -> Void = { _ in }
  208. ) -> Self {
  209. var config = Self(
  210. certificateChain: certificateChain,
  211. privateKey: privateKey,
  212. serverCertificateVerification: .fullVerification,
  213. trustRoots: .systemDefault,
  214. serverHostname: nil
  215. )
  216. configure(&config)
  217. return config
  218. }
  219. }
  220. }