TLSConfiguration.swift 7.2 KB

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