TLSConfiguration.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. self.configuration = .forClient(
  92. minimumTLSVersion: .tlsv12,
  93. certificateVerification: certificateVerification,
  94. trustRoots: trustRoots,
  95. certificateChain: certificateChain,
  96. privateKey: privateKey,
  97. applicationProtocols: GRPCApplicationProtocolIdentifier.client
  98. )
  99. self.hostnameOverride = hostnameOverride
  100. self.customVerificationCallback = customVerificationCallback
  101. }
  102. /// Creates a TLS Configuration using the given `NIOSSL.TLSConfiguration`.
  103. public init(configuration: TLSConfiguration, hostnameOverride: String? = nil) {
  104. self.configuration = configuration
  105. self.hostnameOverride = hostnameOverride
  106. }
  107. }
  108. }
  109. extension Server.Configuration {
  110. /// TLS configuration for a `Server`.
  111. ///
  112. /// Note that this configuration is a subset of `NIOSSL.TLSConfiguration` where certain options
  113. /// are removed from the users control to ensure the configuration complies with the gRPC
  114. /// specification.
  115. public struct TLS {
  116. public private(set) var configuration: TLSConfiguration
  117. /// Whether ALPN is required. Disabling this option may be useful in cases where ALPN is not
  118. /// supported.
  119. public var requireALPN: Bool = true
  120. /// The certificates to offer during negotiation. If not present, no certificates will be
  121. /// offered.
  122. public var certificateChain: [NIOSSLCertificateSource] {
  123. get {
  124. return self.configuration.certificateChain
  125. }
  126. set {
  127. self.configuration.certificateChain = newValue
  128. }
  129. }
  130. /// The private key associated with the leaf certificate.
  131. public var privateKey: NIOSSLPrivateKeySource? {
  132. get {
  133. return self.configuration.privateKey
  134. }
  135. set {
  136. self.configuration.privateKey = newValue
  137. }
  138. }
  139. /// The trust roots to use to validate certificates. This only needs to be provided if you
  140. /// intend to validate certificates.
  141. public var trustRoots: NIOSSLTrustRoots? {
  142. get {
  143. return self.configuration.trustRoots
  144. }
  145. set {
  146. self.configuration.trustRoots = newValue
  147. }
  148. }
  149. /// Whether to verify remote certificates.
  150. public var certificateVerification: CertificateVerification {
  151. get {
  152. return self.configuration.certificateVerification
  153. }
  154. set {
  155. self.configuration.certificateVerification = newValue
  156. }
  157. }
  158. /// TLS Configuration with suitable defaults for servers.
  159. ///
  160. /// This is a wrapper around `NIOSSL.TLSConfiguration` to restrict input to values which comply
  161. /// with the gRPC protocol.
  162. ///
  163. /// - Parameter certificateChain: The certificate to offer during negotiation.
  164. /// - Parameter privateKey: The private key associated with the leaf certificate.
  165. /// - Parameter trustRoots: The trust roots to validate certificates, this defaults to using a
  166. /// root provided by the platform.
  167. /// - Parameter certificateVerification: Whether to verify the remote certificate. Defaults to
  168. /// `.none`.
  169. /// - Parameter requireALPN: Whether ALPN is required or not.
  170. public init(
  171. certificateChain: [NIOSSLCertificateSource],
  172. privateKey: NIOSSLPrivateKeySource,
  173. trustRoots: NIOSSLTrustRoots = .default,
  174. certificateVerification: CertificateVerification = .none,
  175. requireALPN: Bool = true
  176. ) {
  177. self.configuration = .forServer(
  178. certificateChain: certificateChain,
  179. privateKey: privateKey,
  180. minimumTLSVersion: .tlsv12,
  181. certificateVerification: certificateVerification,
  182. trustRoots: trustRoots,
  183. applicationProtocols: GRPCApplicationProtocolIdentifier.server
  184. )
  185. self.requireALPN = requireALPN
  186. }
  187. /// Creates a TLS Configuration using the given `NIOSSL.TLSConfiguration`.
  188. public init(configuration: TLSConfiguration, requireALPN: Bool = true) {
  189. self.configuration = configuration
  190. self.requireALPN = requireALPN
  191. }
  192. }
  193. }