TLSConfig.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. extension HTTP2ServerTransport.Posix.Config {
  116. /// The security configuration for this connection.
  117. public struct TransportSecurity: Sendable {
  118. package enum Wrapped: Sendable {
  119. case plaintext
  120. case tls(TLS)
  121. }
  122. package let wrapped: Wrapped
  123. /// This connection is plaintext: no encryption will take place.
  124. public static let plaintext = Self(wrapped: .plaintext)
  125. /// This connection will use TLS.
  126. public static func tls(_ tls: TLS) -> Self {
  127. Self(wrapped: .tls(tls))
  128. }
  129. }
  130. public struct TLS: Sendable {
  131. /// The certificates the server will offer during negotiation.
  132. public var certificateChain: [TLSConfig.CertificateSource]
  133. /// The private key associated with the leaf certificate.
  134. public var privateKey: TLSConfig.PrivateKeySource
  135. /// How to verify the client certificate, if one is presented.
  136. public var clientCertificateVerification: TLSConfig.CertificateVerification
  137. /// The trust roots to be used when verifying client certificates.
  138. public var trustRoots: TLSConfig.TrustRootsSource
  139. /// Whether ALPN is required.
  140. ///
  141. /// If this is set to `true` but the client does not support ALPN, then the connection will be rejected.
  142. public var requireALPN: Bool
  143. /// Create a new HTTP2 NIO Posix server transport TLS config.
  144. /// - Parameters:
  145. /// - certificateChain: The certificates the server will offer during negotiation.
  146. /// - privateKey: The private key associated with the leaf certificate.
  147. /// - clientCertificateVerification: How to verify the client certificate, if one is presented.
  148. /// - trustRoots: The trust roots to be used when verifying client certificates.
  149. /// - requireALPN: Whether ALPN is required.
  150. public init(
  151. certificateChain: [TLSConfig.CertificateSource],
  152. privateKey: TLSConfig.PrivateKeySource,
  153. clientCertificateVerification: TLSConfig.CertificateVerification,
  154. trustRoots: TLSConfig.TrustRootsSource,
  155. requireALPN: Bool
  156. ) {
  157. self.certificateChain = certificateChain
  158. self.privateKey = privateKey
  159. self.clientCertificateVerification = clientCertificateVerification
  160. self.trustRoots = trustRoots
  161. self.requireALPN = requireALPN
  162. }
  163. /// Create a new HTTP2 NIO Posix transport TLS config, with some values defaulted:
  164. /// - `clientCertificateVerificationMode` equals `doNotVerify`
  165. /// - `trustRoots` equals `systemDefault`
  166. /// - `requireALPN` equals `false`
  167. ///
  168. /// - Parameters:
  169. /// - certificateChain: The certificates the server will offer during negotiation.
  170. /// - privateKey: The private key associated with the leaf certificate.
  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. certificateChain: [TLSConfig.CertificateSource],
  175. privateKey: TLSConfig.PrivateKeySource,
  176. configure: (_ config: inout Self) -> Void = { _ in }
  177. ) -> Self {
  178. var config = Self(
  179. certificateChain: certificateChain,
  180. privateKey: privateKey,
  181. clientCertificateVerification: .noVerification,
  182. trustRoots: .systemDefault,
  183. requireALPN: false
  184. )
  185. configure(&config)
  186. return config
  187. }
  188. /// Create a new HTTP2 NIO Posix transport TLS config, with some values defaulted to match
  189. /// the requirements of mTLS:
  190. /// - `clientCertificateVerificationMode` equals `noHostnameVerification`
  191. /// - `trustRoots` equals `systemDefault`
  192. /// - `requireALPN` equals `false`
  193. ///
  194. /// - Parameters:
  195. /// - certificateChain: The certificates the server will offer during negotiation.
  196. /// - privateKey: The private key associated with the leaf certificate.
  197. /// - configure: A closure which allows you to modify the defaults before returning them.
  198. /// - Returns: A new HTTP2 NIO Posix transport TLS config.
  199. public static func mTLS(
  200. certificateChain: [TLSConfig.CertificateSource],
  201. privateKey: TLSConfig.PrivateKeySource,
  202. configure: (_ config: inout Self) -> Void = { _ in }
  203. ) -> Self {
  204. var config = Self(
  205. certificateChain: certificateChain,
  206. privateKey: privateKey,
  207. clientCertificateVerification: .noHostnameVerification,
  208. trustRoots: .systemDefault,
  209. requireALPN: false
  210. )
  211. configure(&config)
  212. return config
  213. }
  214. }
  215. }
  216. extension HTTP2ClientTransport.Posix.Config {
  217. /// The security configuration for this connection.
  218. public struct TransportSecurity: Sendable {
  219. package enum Wrapped: Sendable {
  220. case plaintext
  221. case tls(TLS)
  222. }
  223. package let wrapped: Wrapped
  224. /// This connection is plaintext: no encryption will take place.
  225. public static let plaintext = Self(wrapped: .plaintext)
  226. /// This connection will use TLS.
  227. public static func tls(_ tls: TLS) -> Self {
  228. Self(wrapped: .tls(tls))
  229. }
  230. }
  231. public struct TLS: Sendable {
  232. /// The certificates the client will offer during negotiation.
  233. public var certificateChain: [TLSConfig.CertificateSource]
  234. /// The private key associated with the leaf certificate.
  235. public var privateKey: TLSConfig.PrivateKeySource?
  236. /// How to verify the server certificate, if one is presented.
  237. public var serverCertificateVerification: TLSConfig.CertificateVerification
  238. /// The trust roots to be used when verifying server certificates.
  239. public var trustRoots: TLSConfig.TrustRootsSource
  240. /// An optional server hostname to use when verifying certificates.
  241. public var serverHostname: String?
  242. /// Create a new HTTP2 NIO Posix client transport TLS config.
  243. /// - Parameters:
  244. /// - certificateChain: The certificates the client will offer during negotiation.
  245. /// - privateKey: The private key associated with the leaf certificate.
  246. /// - serverCertificateVerification: How to verify the server certificate, if one is presented.
  247. /// - trustRoots: The trust roots to be used when verifying server certificates.
  248. /// - serverHostname: An optional server hostname to use when verifying certificates.
  249. public init(
  250. certificateChain: [TLSConfig.CertificateSource],
  251. privateKey: TLSConfig.PrivateKeySource?,
  252. serverCertificateVerification: TLSConfig.CertificateVerification,
  253. trustRoots: TLSConfig.TrustRootsSource,
  254. serverHostname: String?
  255. ) {
  256. self.certificateChain = certificateChain
  257. self.privateKey = privateKey
  258. self.serverCertificateVerification = serverCertificateVerification
  259. self.trustRoots = trustRoots
  260. self.serverHostname = serverHostname
  261. }
  262. /// Create a new HTTP2 NIO Posix transport TLS config, with some values defaulted:
  263. /// - `certificateChain` equals `[]`
  264. /// - `privateKey` equals `nil`
  265. /// - `serverCertificateVerification` equals `fullVerification`
  266. /// - `trustRoots` equals `systemDefault`
  267. /// - `serverHostname` equals `nil`
  268. ///
  269. /// - Parameters:
  270. /// - configure: A closure which allows you to modify the defaults before returning them.
  271. /// - Returns: A new HTTP2 NIO Posix transport TLS config.
  272. public static func defaults(
  273. configure: (_ config: inout Self) -> Void = { _ in }
  274. ) -> Self {
  275. var config = Self(
  276. certificateChain: [],
  277. privateKey: nil,
  278. serverCertificateVerification: .fullVerification,
  279. trustRoots: .systemDefault,
  280. serverHostname: nil
  281. )
  282. configure(&config)
  283. return config
  284. }
  285. /// Create a new HTTP2 NIO Posix transport TLS config, with some values defaulted:
  286. /// - `certificateChain` equals `[]`
  287. /// - `privateKey` equals `nil`
  288. /// - `serverCertificateVerification` equals `fullVerification`
  289. /// - `trustRoots` equals `systemDefault`
  290. /// - `serverHostname` equals `nil`
  291. public static var defaults: Self {
  292. Self.defaults()
  293. }
  294. /// Create a new HTTP2 NIO Posix transport TLS config, with some values defaulted to match
  295. /// the requirements of mTLS:
  296. /// - `trustRoots` equals `systemDefault`
  297. /// - `serverCertificateVerification` equals `fullVerification`
  298. ///
  299. /// - Parameters:
  300. /// - certificateChain: The certificates the client will offer during negotiation.
  301. /// - privateKey: The private key associated with the leaf certificate.
  302. /// - configure: A closure which allows you to modify the defaults before returning them.
  303. /// - Returns: A new HTTP2 NIO Posix transport TLS config.
  304. public static func mTLS(
  305. certificateChain: [TLSConfig.CertificateSource],
  306. privateKey: TLSConfig.PrivateKeySource,
  307. configure: (_ config: inout Self) -> Void = { _ in }
  308. ) -> Self {
  309. var config = Self(
  310. certificateChain: certificateChain,
  311. privateKey: privateKey,
  312. serverCertificateVerification: .fullVerification,
  313. trustRoots: .systemDefault,
  314. serverHostname: nil
  315. )
  316. configure(&config)
  317. return config
  318. }
  319. }
  320. }