Config+TLS.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 GRPCNIOTransportCore
  18. public import Network
  19. private import struct Foundation.Data
  20. private import struct Foundation.URL
  21. @available(gRPCSwiftNIOTransport 2.0, *)
  22. extension HTTP2ServerTransport.TransportServices {
  23. /// The security configuration for this connection.
  24. public struct TransportSecurity: Sendable {
  25. package enum Wrapped: Sendable {
  26. case plaintext
  27. case tls(TLS)
  28. }
  29. package let wrapped: Wrapped
  30. /// This connection is plaintext: no encryption will take place.
  31. public static let plaintext = Self(wrapped: .plaintext)
  32. /// Secures connections with the given TLS configuration.
  33. public static func tls(_ tls: TLS) -> Self {
  34. Self(wrapped: .tls(tls))
  35. }
  36. /// Secures connections with the TLS.
  37. ///
  38. /// - Parameters:
  39. /// - identityProvider: A provider for the `SecIdentity` to be used when setting up TLS.
  40. /// - configure: A closure allowing you to modify the configuration before returning it.
  41. public static func tls(
  42. identityProvider: @Sendable @escaping () throws -> SecIdentity,
  43. configure: (_ config: inout TLS) -> Void = { _ in }
  44. ) -> Self {
  45. let tlsConfig: TLS = .defaults(
  46. identityProvider: identityProvider,
  47. configure: configure
  48. )
  49. return .tls(tlsConfig)
  50. }
  51. /// Secures connections with the mutual TLS.
  52. ///
  53. /// - Parameters:
  54. /// - identityProvider: A provider for the `SecIdentity` to be used when setting up TLS.
  55. /// - configure: A closure allowing you to modify the configuration before returning it.
  56. public static func mTLS(
  57. identityProvider: @Sendable @escaping () throws -> SecIdentity,
  58. configure: (_ config: inout TLS) -> Void = { _ in }
  59. ) -> Self {
  60. let tlsConfig: TLS = .mTLS(
  61. identityProvider: identityProvider,
  62. configure: configure
  63. )
  64. return .tls(tlsConfig)
  65. }
  66. }
  67. }
  68. @available(gRPCSwiftNIOTransport 2.0, *)
  69. extension HTTP2ServerTransport.TransportServices {
  70. public struct TLS: Sendable {
  71. /// How to verify the client certificate, if one is presented.
  72. public var clientCertificateVerification: TLSConfig.CertificateVerification
  73. /// The trust roots to be used when verifying client certificates.
  74. public var trustRoots: TLSConfig.TrustRootsSource
  75. /// Whether ALPN is required.
  76. ///
  77. /// If this is set to `true` but the client does not support ALPN, then the connection will be rejected.
  78. public var requireALPN: Bool
  79. /// A provider for the `SecIdentity` to be used when setting up TLS.
  80. public var identityProvider: @Sendable () throws -> SecIdentity
  81. /// Create a new HTTP2 NIO Transport Services transport TLS config.
  82. /// - Parameters:
  83. /// - clientCertificateVerification: How to verify the client certificate, if one is presented.
  84. /// - trustRoots: The trust roots to be used when verifying client certificates.
  85. /// - requireALPN: Whether ALPN is required.
  86. /// - identityProvider: A provider for the `SecIdentity` to be used when setting up TLS.
  87. public init(
  88. clientCertificateVerification: TLSConfig.CertificateVerification,
  89. trustRoots: TLSConfig.TrustRootsSource,
  90. requireALPN: Bool,
  91. identityProvider: @Sendable @escaping () throws -> SecIdentity
  92. ) {
  93. self.clientCertificateVerification = clientCertificateVerification
  94. self.trustRoots = trustRoots
  95. self.requireALPN = requireALPN
  96. self.identityProvider = identityProvider
  97. }
  98. /// Create a new HTTP2 NIO Transport Services transport TLS config, with some values defaulted:
  99. /// - `clientCertificateVerificationMode` equals `doNotVerify`
  100. /// - `trustRoots` equals `systemDefault`
  101. /// - `requireALPN` equals `false`
  102. ///
  103. /// - Parameters:
  104. /// - identityProvider: A provider for the `SecIdentity` to be used when setting up TLS.
  105. /// - configure: A closure which allows you to modify the defaults before returning them.
  106. /// - Returns: A new HTTP2 NIO Transport Services transport TLS config.
  107. public static func defaults(
  108. identityProvider: @Sendable @escaping () throws -> SecIdentity,
  109. configure: (_ config: inout Self) -> Void = { _ in }
  110. ) -> Self {
  111. var config = Self(
  112. clientCertificateVerification: .noVerification,
  113. trustRoots: .systemDefault,
  114. requireALPN: false,
  115. identityProvider: identityProvider
  116. )
  117. configure(&config)
  118. return config
  119. }
  120. /// Create a new HTTP2 NIO Transport Services transport TLS config, with some values defaulted to match
  121. /// the requirements of mTLS:
  122. /// - `clientCertificateVerificationMode` equals `noHostnameVerification`
  123. /// - `trustRoots` equals `systemDefault`
  124. /// - `requireALPN` equals `false`
  125. ///
  126. /// - Parameters:
  127. /// - identityProvider: A provider for the `SecIdentity` to be used when setting up TLS.
  128. /// - configure: A closure which allows you to modify the defaults before returning them.
  129. /// - Returns: A new HTTP2 NIO Transport Services transport TLS config.
  130. public static func mTLS(
  131. identityProvider: @Sendable @escaping () throws -> SecIdentity,
  132. configure: (_ config: inout Self) -> Void = { _ in }
  133. ) -> Self {
  134. var config = Self(
  135. clientCertificateVerification: .noHostnameVerification,
  136. trustRoots: .systemDefault,
  137. requireALPN: false,
  138. identityProvider: identityProvider
  139. )
  140. configure(&config)
  141. return config
  142. }
  143. }
  144. }
  145. @available(gRPCSwiftNIOTransport 2.0, *)
  146. extension HTTP2ClientTransport.TransportServices {
  147. /// The security configuration for this connection.
  148. public struct TransportSecurity: Sendable {
  149. package enum Wrapped: Sendable {
  150. case plaintext
  151. case tls(TLS)
  152. }
  153. package let wrapped: Wrapped
  154. /// This connection is plaintext: no encryption will take place.
  155. public static let plaintext = Self(wrapped: .plaintext)
  156. /// Secure connections with the given TLS configuration.
  157. public static func tls(_ tls: TLS) -> Self {
  158. Self(wrapped: .tls(tls))
  159. }
  160. /// Secure connections with TLS.
  161. ///
  162. /// - Parameters:
  163. /// - configure: A closure which allows you to modify the defaults before returning them.
  164. public static func tls(
  165. configure: (_ config: inout TLS) -> Void = { _ in }
  166. ) -> Self {
  167. let tlsConfig: TLS = .defaults(configure: configure)
  168. return .tls(tlsConfig)
  169. }
  170. /// Secure connections with TLS.
  171. public static var tls: Self {
  172. return .tls()
  173. }
  174. /// Secure connections with mutual TLS.
  175. ///
  176. /// - Parameters:
  177. /// - identityProvider: A provider for the `SecIdentity` to be used when setting up TLS.
  178. /// - configure: A closure which allows you to modify the defaults before returning them.
  179. public static func mTLS(
  180. identityProvider: @Sendable @escaping () throws -> SecIdentity,
  181. configure: (_ config: inout TLS) -> Void = { _ in }
  182. ) -> Self {
  183. let tlsConfig: TLS = .mTLS(
  184. identityProvider: identityProvider,
  185. configure: configure
  186. )
  187. return .tls(tlsConfig)
  188. }
  189. }
  190. }
  191. @available(gRPCSwiftNIOTransport 2.0, *)
  192. extension HTTP2ClientTransport.TransportServices {
  193. public struct TLS: Sendable {
  194. /// How to verify the server certificate, if one is presented.
  195. public var serverCertificateVerification: TLSConfig.CertificateVerification
  196. /// The trust roots to be used when verifying server certificates.
  197. /// - Important: If specifying custom certificates, they must be DER-encoded X509 certificates.
  198. public var trustRoots: TLSConfig.TrustRootsSource
  199. /// An optional provider for the `SecIdentity` to be used when setting up TLS.
  200. public var identityProvider: (@Sendable () throws -> SecIdentity)?
  201. /// Create a new HTTP2 NIO Transport Services transport TLS config.
  202. /// - Parameters:
  203. /// - serverCertificateVerification: How to verify the server certificate, if one is presented.
  204. /// - trustRoots: The trust roots to be used when verifying server certificates.
  205. /// - identityProvider: A provider for the `SecIdentity` to be used when setting up TLS.
  206. public init(
  207. serverCertificateVerification: TLSConfig.CertificateVerification,
  208. trustRoots: TLSConfig.TrustRootsSource,
  209. identityProvider: (@Sendable () throws -> SecIdentity)?
  210. ) {
  211. self.serverCertificateVerification = serverCertificateVerification
  212. self.trustRoots = trustRoots
  213. self.identityProvider = identityProvider
  214. }
  215. /// Create a new HTTP2 NIO Transport Services transport TLS config, with some values defaulted:
  216. /// - `serverCertificateVerification` equals `fullVerification`
  217. /// - `trustRoots` equals `systemDefault`
  218. /// - `identityProvider` equals `nil`
  219. ///
  220. /// - Parameters:
  221. /// - configure: A closure which allows you to modify the defaults before returning them.
  222. /// - Returns: A new HTTP2 NIO Posix transport TLS config.
  223. public static func defaults(
  224. configure: (_ config: inout Self) -> Void = { _ in }
  225. ) -> Self {
  226. var config = Self(
  227. serverCertificateVerification: .fullVerification,
  228. trustRoots: .systemDefault,
  229. identityProvider: nil
  230. )
  231. configure(&config)
  232. return config
  233. }
  234. /// Create a new HTTP2 NIO Transport Services transport TLS config, with some values defaulted:
  235. /// - `serverCertificateVerification` equals `fullVerification`
  236. /// - `trustRoots` equals `systemDefault`
  237. /// - `identityProvider` equals `nil`
  238. public static var defaults: Self { .defaults() }
  239. /// Create a new HTTP2 NIO Transport Services transport TLS config, with some values defaulted to match
  240. /// the requirements of mTLS:
  241. /// - `serverCertificateVerification` equals `fullVerification`
  242. /// - `trustRoots` equals `systemDefault`
  243. ///
  244. /// - Parameters:
  245. /// - identityProvider: A provider for the `SecIdentity` to be used when setting up TLS.
  246. /// - configure: A closure which allows you to modify the defaults before returning them.
  247. /// - Returns: A new HTTP2 NIO Posix transport TLS config.
  248. public static func mTLS(
  249. identityProvider: @Sendable @escaping () throws -> SecIdentity,
  250. configure: (_ config: inout Self) -> Void = { _ in }
  251. ) -> Self {
  252. var config = Self(
  253. serverCertificateVerification: .fullVerification,
  254. trustRoots: .systemDefault,
  255. identityProvider: identityProvider
  256. )
  257. configure(&config)
  258. return config
  259. }
  260. }
  261. }
  262. @available(gRPCSwiftNIOTransport 2.0, *)
  263. extension NWProtocolTLS.Options {
  264. func setUpVerifyBlock(trustRootsSource: TLSConfig.TrustRootsSource) {
  265. if let (verifyQueue, verifyBlock) = trustRootsSource.makeTrustRootsConfig() {
  266. sec_protocol_options_set_verify_block(
  267. self.securityProtocolOptions,
  268. verifyBlock,
  269. verifyQueue
  270. )
  271. }
  272. }
  273. }
  274. @available(gRPCSwiftNIOTransport 2.0, *)
  275. extension TLSConfig.TrustRootsSource {
  276. internal func makeTrustRootsConfig() -> (DispatchQueue, sec_protocol_verify_t)? {
  277. switch self.wrapped {
  278. case .certificates(let certificates):
  279. let verifyQueue = DispatchQueue(label: "io.grpc.CertificateVerification")
  280. let verifyBlock: sec_protocol_verify_t = { (metadata, trust, verifyCompleteCallback) in
  281. let actualTrust = sec_trust_copy_ref(trust).takeRetainedValue()
  282. let customAnchors: [SecCertificate]
  283. do {
  284. customAnchors = try certificates.map { certificateSource in
  285. let certificateBytes: Data
  286. switch certificateSource.wrapped {
  287. case .file(let path, .der):
  288. certificateBytes = try Data(contentsOf: URL(filePath: path))
  289. case .bytes(let bytes, .der):
  290. certificateBytes = Data(bytes)
  291. case .file(_, let format), .bytes(_, let format):
  292. fatalError("Certificate format must be DER, but was \(format).")
  293. case .transportSpecific:
  294. fatalError("Certificate format must be DER, but was an unsupported type.")
  295. }
  296. guard let certificate = SecCertificateCreateWithData(nil, certificateBytes as CFData)
  297. else {
  298. fatalError("Certificate was not a valid DER-encoded X509 certificate.")
  299. }
  300. return certificate
  301. }
  302. } catch {
  303. verifyCompleteCallback(false)
  304. return
  305. }
  306. SecTrustSetAnchorCertificates(actualTrust, customAnchors as CFArray)
  307. SecTrustEvaluateAsyncWithError(actualTrust, verifyQueue) { _, trusted, _ in
  308. verifyCompleteCallback(trusted)
  309. }
  310. }
  311. return (verifyQueue, verifyBlock)
  312. case .systemDefault:
  313. return nil
  314. }
  315. }
  316. }
  317. #endif