TLSConfig.swift 14 KB

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