TLSConfiguration.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.allCases.map { $0.rawValue }
  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. /// The certificates to offer during negotiation. If not present, no certificates will be
  112. /// offered.
  113. public var certificateChain: [NIOSSLCertificateSource] {
  114. get {
  115. return self.configuration.certificateChain
  116. }
  117. set {
  118. self.configuration.certificateChain = newValue
  119. }
  120. }
  121. /// The private key associated with the leaf certificate.
  122. public var privateKey: NIOSSLPrivateKeySource? {
  123. get {
  124. return self.configuration.privateKey
  125. }
  126. set {
  127. self.configuration.privateKey = newValue
  128. }
  129. }
  130. /// The trust roots to use to validate certificates. This only needs to be provided if you
  131. /// intend to validate certificates.
  132. public var trustRoots: NIOSSLTrustRoots? {
  133. get {
  134. return self.configuration.trustRoots
  135. }
  136. set {
  137. self.configuration.trustRoots = newValue
  138. }
  139. }
  140. /// Whether to verify remote certificates.
  141. public var certificateVerification: CertificateVerification {
  142. get {
  143. return self.configuration.certificateVerification
  144. }
  145. set {
  146. self.configuration.certificateVerification = newValue
  147. }
  148. }
  149. /// TLS Configuration with suitable defaults for clients.
  150. ///
  151. /// This is a wrapper around `NIOSSL.TLSConfiguration` to restrict input to values which comply
  152. /// with the gRPC protocol.
  153. ///
  154. /// - Parameter certificateChain: The certificate to offer during negotiation.
  155. /// - Parameter privateKey: The private key associated with the leaf certificate.
  156. /// - Parameter trustRoots: The trust roots to validate certificates, this defaults to using a
  157. /// root provided by the platform.
  158. /// - Parameter certificateVerification: Whether to verify the remote certificate. Defaults to
  159. /// `.none`.
  160. public init(
  161. certificateChain: [NIOSSLCertificateSource],
  162. privateKey: NIOSSLPrivateKeySource,
  163. trustRoots: NIOSSLTrustRoots = .default,
  164. certificateVerification: CertificateVerification = .none
  165. ) {
  166. self.configuration = .forServer(
  167. certificateChain: certificateChain,
  168. privateKey: privateKey,
  169. minimumTLSVersion: .tlsv12,
  170. certificateVerification: certificateVerification,
  171. trustRoots: trustRoots,
  172. applicationProtocols: GRPCApplicationProtocolIdentifier.allCases.map { $0.rawValue }
  173. )
  174. }
  175. /// Creates a TLS Configuration using the given `NIOSSL.TLSConfiguration`.
  176. public init(configuration: TLSConfiguration) {
  177. self.configuration = configuration
  178. }
  179. }
  180. }