TLSConfiguration.swift 9.0 KB

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