TLSConfiguration.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright 2019, 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. import NIOSSL
  17. extension ClientConnection.Configuration {
  18. /// TLS configuration for a `ClientConnection`.
  19. ///
  20. /// Note that this configuration is a subset of `NIOSSL.TLSConfiguration` where certain options
  21. /// are removed from the user's control to ensure the configuration complies with the gRPC
  22. /// specification.
  23. @available(*, deprecated, renamed: "GRPCTLSConfiguration")
  24. public struct TLS {
  25. public private(set) var configuration: TLSConfiguration
  26. /// Value to use for TLS SNI extension; this must not be an address.
  27. public var hostnameOverride: String?
  28. /// The certificates to offer during negotiation. If not present, no certificates will be offered.
  29. public var certificateChain: [NIOSSLCertificateSource] {
  30. get {
  31. return self.configuration.certificateChain
  32. }
  33. set {
  34. self.configuration.certificateChain = newValue
  35. }
  36. }
  37. /// The private key associated with the leaf certificate.
  38. public var privateKey: NIOSSLPrivateKeySource? {
  39. get {
  40. return self.configuration.privateKey
  41. }
  42. set {
  43. self.configuration.privateKey = newValue
  44. }
  45. }
  46. /// The trust roots to use to validate certificates. This only needs to be provided if you
  47. /// intend to validate certificates.
  48. public var trustRoots: NIOSSLTrustRoots? {
  49. get {
  50. return self.configuration.trustRoots
  51. }
  52. set {
  53. self.configuration.trustRoots = newValue
  54. }
  55. }
  56. /// Whether to verify remote certificates.
  57. public var certificateVerification: CertificateVerification {
  58. get {
  59. return self.configuration.certificateVerification
  60. }
  61. set {
  62. self.configuration.certificateVerification = newValue
  63. }
  64. }
  65. /// A custom verification callback that allows completely overriding the certificate verification logic for this connection.
  66. public var customVerificationCallback: NIOSSLCustomVerificationCallback?
  67. /// TLS Configuration with suitable defaults for clients.
  68. ///
  69. /// This is a wrapper around `NIOSSL.TLSConfiguration` to restrict input to values which comply
  70. /// with the gRPC protocol.
  71. ///
  72. /// - Parameter certificateChain: The certificate to offer during negotiation, defaults to an
  73. /// empty array.
  74. /// - Parameter privateKey: The private key associated with the leaf certificate. This defaults
  75. /// to `nil`.
  76. /// - Parameter trustRoots: The trust roots to validate certificates, this defaults to using a
  77. /// root provided by the platform.
  78. /// - Parameter certificateVerification: Whether to verify the remote certificate. Defaults to
  79. /// `.fullVerification`.
  80. /// - Parameter hostnameOverride: Value to use for TLS SNI extension; this must not be an IP
  81. /// address, defaults to `nil`.
  82. /// - Parameter customVerificationCallback: A callback to provide to override the certificate verification logic,
  83. /// defaults to `nil`.
  84. public init(
  85. certificateChain: [NIOSSLCertificateSource] = [],
  86. privateKey: NIOSSLPrivateKeySource? = nil,
  87. trustRoots: NIOSSLTrustRoots = .default,
  88. certificateVerification: CertificateVerification = .fullVerification,
  89. hostnameOverride: String? = nil,
  90. customVerificationCallback: NIOSSLCustomVerificationCallback? = nil
  91. ) {
  92. var configuration = TLSConfiguration.makeClientConfiguration()
  93. configuration.minimumTLSVersion = .tlsv12
  94. configuration.certificateVerification = certificateVerification
  95. configuration.trustRoots = trustRoots
  96. configuration.certificateChain = certificateChain
  97. configuration.privateKey = privateKey
  98. configuration.applicationProtocols = GRPCApplicationProtocolIdentifier.client
  99. self.configuration = configuration
  100. self.hostnameOverride = hostnameOverride
  101. self.customVerificationCallback = customVerificationCallback
  102. }
  103. /// Creates a TLS Configuration using the given `NIOSSL.TLSConfiguration`.
  104. ///
  105. /// - Note: If no ALPN tokens are set in `configuration.applicationProtocols` then "grpc-exp"
  106. /// and "h2" will be used.
  107. /// - Parameters:
  108. /// - configuration: The `NIOSSL.TLSConfiguration` to base this configuration on.
  109. /// - hostnameOverride: The hostname override to use for the TLS SNI extension.
  110. public init(configuration: TLSConfiguration, hostnameOverride: String? = nil) {
  111. self.configuration = configuration
  112. self.hostnameOverride = hostnameOverride
  113. // Set the ALPN tokens if none were set.
  114. if self.configuration.applicationProtocols.isEmpty {
  115. self.configuration.applicationProtocols = GRPCApplicationProtocolIdentifier.client
  116. }
  117. }
  118. }
  119. }
  120. extension Server.Configuration {
  121. /// TLS configuration for a `Server`.
  122. ///
  123. /// Note that this configuration is a subset of `NIOSSL.TLSConfiguration` where certain options
  124. /// are removed from the users control to ensure the configuration complies with the gRPC
  125. /// specification.
  126. @available(*, deprecated, renamed: "GRPCTLSConfiguration")
  127. public struct TLS {
  128. public private(set) var configuration: TLSConfiguration
  129. /// Whether ALPN is required. Disabling this option may be useful in cases where ALPN is not
  130. /// supported.
  131. public var requireALPN: Bool = true
  132. /// The certificates to offer during negotiation. If not present, no certificates will be
  133. /// offered.
  134. public var certificateChain: [NIOSSLCertificateSource] {
  135. get {
  136. return self.configuration.certificateChain
  137. }
  138. set {
  139. self.configuration.certificateChain = newValue
  140. }
  141. }
  142. /// The private key associated with the leaf certificate.
  143. public var privateKey: NIOSSLPrivateKeySource? {
  144. get {
  145. return self.configuration.privateKey
  146. }
  147. set {
  148. self.configuration.privateKey = newValue
  149. }
  150. }
  151. /// The trust roots to use to validate certificates. This only needs to be provided if you
  152. /// intend to validate certificates.
  153. public var trustRoots: NIOSSLTrustRoots? {
  154. get {
  155. return self.configuration.trustRoots
  156. }
  157. set {
  158. self.configuration.trustRoots = newValue
  159. }
  160. }
  161. /// Whether to verify remote certificates.
  162. public var certificateVerification: CertificateVerification {
  163. get {
  164. return self.configuration.certificateVerification
  165. }
  166. set {
  167. self.configuration.certificateVerification = newValue
  168. }
  169. }
  170. /// TLS Configuration with suitable defaults for servers.
  171. ///
  172. /// This is a wrapper around `NIOSSL.TLSConfiguration` to restrict input to values which comply
  173. /// with the gRPC protocol.
  174. ///
  175. /// - Parameter certificateChain: The certificate to offer during negotiation.
  176. /// - Parameter privateKey: The private key associated with the leaf certificate.
  177. /// - Parameter trustRoots: The trust roots to validate certificates, this defaults to using a
  178. /// root provided by the platform.
  179. /// - Parameter certificateVerification: Whether to verify the remote certificate. Defaults to
  180. /// `.none`.
  181. /// - Parameter requireALPN: Whether ALPN is required or not.
  182. public init(
  183. certificateChain: [NIOSSLCertificateSource],
  184. privateKey: NIOSSLPrivateKeySource,
  185. trustRoots: NIOSSLTrustRoots = .default,
  186. certificateVerification: CertificateVerification = .none,
  187. requireALPN: Bool = true
  188. ) {
  189. var configuration = TLSConfiguration.makeServerConfiguration(
  190. certificateChain: certificateChain,
  191. privateKey: privateKey
  192. )
  193. configuration.minimumTLSVersion = .tlsv12
  194. configuration.certificateVerification = certificateVerification
  195. configuration.trustRoots = trustRoots
  196. configuration.applicationProtocols = GRPCApplicationProtocolIdentifier.server
  197. self.configuration = configuration
  198. self.requireALPN = requireALPN
  199. }
  200. /// Creates a TLS Configuration using the given `NIOSSL.TLSConfiguration`.
  201. /// - Note: If no ALPN tokens are set in `configuration.applicationProtocols` then the tokens
  202. /// "grpc-exp", "h2" and "http/1.1" will be used.
  203. /// - Parameters:
  204. /// - configuration: The `NIOSSL.TLSConfiguration` to base this configuration on.
  205. /// - requireALPN: Whether ALPN is required.
  206. public init(configuration: TLSConfiguration, requireALPN: Bool = true) {
  207. self.configuration = configuration
  208. self.requireALPN = requireALPN
  209. // Set the ALPN tokens if none were set.
  210. if self.configuration.applicationProtocols.isEmpty {
  211. self.configuration.applicationProtocols = GRPCApplicationProtocolIdentifier.server
  212. }
  213. }
  214. }
  215. }