ClientConnectionConfiguration+NIOSSL.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #if canImport(NIOSSL)
  17. import NIOSSL
  18. extension ClientConnection.Configuration {
  19. /// TLS configuration for a `ClientConnection`.
  20. ///
  21. /// Note that this configuration is a subset of `NIOSSL.TLSConfiguration` where certain options
  22. /// are removed from the user's control to ensure the configuration complies with the gRPC
  23. /// specification.
  24. @available(*, deprecated, renamed: "GRPCTLSConfiguration")
  25. public struct TLS {
  26. public private(set) var configuration: TLSConfiguration
  27. /// Value to use for TLS SNI extension; this must not be an address.
  28. public var hostnameOverride: String?
  29. /// The certificates to offer during negotiation. If not present, no certificates will be offered.
  30. public var certificateChain: [NIOSSLCertificateSource] {
  31. get {
  32. return self.configuration.certificateChain
  33. }
  34. set {
  35. self.configuration.certificateChain = newValue
  36. }
  37. }
  38. /// The private key associated with the leaf certificate.
  39. public var privateKey: NIOSSLPrivateKeySource? {
  40. get {
  41. return self.configuration.privateKey
  42. }
  43. set {
  44. self.configuration.privateKey = newValue
  45. }
  46. }
  47. /// The trust roots to use to validate certificates. This only needs to be provided if you
  48. /// intend to validate certificates.
  49. public var trustRoots: NIOSSLTrustRoots? {
  50. get {
  51. return self.configuration.trustRoots
  52. }
  53. set {
  54. self.configuration.trustRoots = newValue
  55. }
  56. }
  57. /// Whether to verify remote certificates.
  58. public var certificateVerification: CertificateVerification {
  59. get {
  60. return self.configuration.certificateVerification
  61. }
  62. set {
  63. self.configuration.certificateVerification = newValue
  64. }
  65. }
  66. /// A custom verification callback that allows completely overriding the certificate verification logic for this connection.
  67. public var customVerificationCallback: NIOSSLCustomVerificationCallback?
  68. /// TLS Configuration with suitable defaults for clients.
  69. ///
  70. /// This is a wrapper around `NIOSSL.TLSConfiguration` to restrict input to values which comply
  71. /// with the gRPC protocol.
  72. ///
  73. /// - Parameter certificateChain: The certificate to offer during negotiation, defaults to an
  74. /// empty array.
  75. /// - Parameter privateKey: The private key associated with the leaf certificate. This defaults
  76. /// to `nil`.
  77. /// - Parameter trustRoots: The trust roots to validate certificates, this defaults to using a
  78. /// root provided by the platform.
  79. /// - Parameter certificateVerification: Whether to verify the remote certificate. Defaults to
  80. /// `.fullVerification`.
  81. /// - Parameter hostnameOverride: Value to use for TLS SNI extension; this must not be an IP
  82. /// address, defaults to `nil`.
  83. /// - Parameter customVerificationCallback: A callback to provide to override the certificate verification logic,
  84. /// defaults to `nil`.
  85. public init(
  86. certificateChain: [NIOSSLCertificateSource] = [],
  87. privateKey: NIOSSLPrivateKeySource? = nil,
  88. trustRoots: NIOSSLTrustRoots = .default,
  89. certificateVerification: CertificateVerification = .fullVerification,
  90. hostnameOverride: String? = nil,
  91. customVerificationCallback: NIOSSLCustomVerificationCallback? = nil
  92. ) {
  93. var configuration = TLSConfiguration.makeClientConfiguration()
  94. configuration.minimumTLSVersion = .tlsv12
  95. configuration.certificateVerification = certificateVerification
  96. configuration.trustRoots = trustRoots
  97. configuration.certificateChain = certificateChain
  98. configuration.privateKey = privateKey
  99. configuration.applicationProtocols = GRPCApplicationProtocolIdentifier.client
  100. self.configuration = configuration
  101. self.hostnameOverride = hostnameOverride
  102. self.customVerificationCallback = customVerificationCallback
  103. }
  104. /// Creates a TLS Configuration using the given `NIOSSL.TLSConfiguration`.
  105. ///
  106. /// - Note: If no ALPN tokens are set in `configuration.applicationProtocols` then "grpc-exp"
  107. /// and "h2" will be used.
  108. /// - Parameters:
  109. /// - configuration: The `NIOSSL.TLSConfiguration` to base this configuration on.
  110. /// - hostnameOverride: The hostname override to use for the TLS SNI extension.
  111. public init(configuration: TLSConfiguration, hostnameOverride: String? = nil) {
  112. self.configuration = configuration
  113. self.hostnameOverride = hostnameOverride
  114. // Set the ALPN tokens if none were set.
  115. if self.configuration.applicationProtocols.isEmpty {
  116. self.configuration.applicationProtocols = GRPCApplicationProtocolIdentifier.client
  117. }
  118. }
  119. }
  120. }
  121. #endif // canImport(NIOSSL)