Server+NIOSSL.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 Server.Configuration {
  19. /// TLS configuration for a ``Server``.
  20. ///
  21. /// Note that this configuration is a subset of `NIOSSL.TLSConfiguration` where certain options
  22. /// are removed from the users 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. /// Whether ALPN is required. Disabling this option may be useful in cases where ALPN is not
  28. /// supported.
  29. public var requireALPN: Bool = true
  30. /// The certificates to offer during negotiation. If not present, no certificates will be
  31. /// offered.
  32. public var certificateChain: [NIOSSLCertificateSource] {
  33. get {
  34. return self.configuration.certificateChain
  35. }
  36. set {
  37. self.configuration.certificateChain = newValue
  38. }
  39. }
  40. /// The private key associated with the leaf certificate.
  41. public var privateKey: NIOSSLPrivateKeySource? {
  42. get {
  43. return self.configuration.privateKey
  44. }
  45. set {
  46. self.configuration.privateKey = newValue
  47. }
  48. }
  49. /// The trust roots to use to validate certificates. This only needs to be provided if you
  50. /// intend to validate certificates.
  51. public var trustRoots: NIOSSLTrustRoots? {
  52. get {
  53. return self.configuration.trustRoots
  54. }
  55. set {
  56. self.configuration.trustRoots = newValue
  57. }
  58. }
  59. /// Whether to verify remote certificates.
  60. public var certificateVerification: CertificateVerification {
  61. get {
  62. return self.configuration.certificateVerification
  63. }
  64. set {
  65. self.configuration.certificateVerification = newValue
  66. }
  67. }
  68. /// TLS Configuration with suitable defaults for servers.
  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.
  74. /// - Parameter privateKey: The private key associated with the leaf certificate.
  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. /// `.none`.
  79. /// - Parameter requireALPN: Whether ALPN is required or not.
  80. public init(
  81. certificateChain: [NIOSSLCertificateSource],
  82. privateKey: NIOSSLPrivateKeySource,
  83. trustRoots: NIOSSLTrustRoots = .default,
  84. certificateVerification: CertificateVerification = .none,
  85. requireALPN: Bool = true
  86. ) {
  87. var configuration = TLSConfiguration.makeServerConfiguration(
  88. certificateChain: certificateChain,
  89. privateKey: privateKey
  90. )
  91. configuration.minimumTLSVersion = .tlsv12
  92. configuration.certificateVerification = certificateVerification
  93. configuration.trustRoots = trustRoots
  94. configuration.applicationProtocols = GRPCApplicationProtocolIdentifier.server
  95. self.configuration = configuration
  96. self.requireALPN = requireALPN
  97. }
  98. /// Creates a TLS Configuration using the given `NIOSSL.TLSConfiguration`.
  99. /// - Note: If no ALPN tokens are set in `configuration.applicationProtocols` then the tokens
  100. /// "grpc-exp", "h2" and "http/1.1" will be used.
  101. /// - Parameters:
  102. /// - configuration: The `NIOSSL.TLSConfiguration` to base this configuration on.
  103. /// - requireALPN: Whether ALPN is required.
  104. public init(configuration: TLSConfiguration, requireALPN: Bool = true) {
  105. self.configuration = configuration
  106. self.requireALPN = requireALPN
  107. // Set the ALPN tokens if none were set.
  108. if self.configuration.applicationProtocols.isEmpty {
  109. self.configuration.applicationProtocols = GRPCApplicationProtocolIdentifier.server
  110. }
  111. }
  112. }
  113. }
  114. #endif // canImport(NIOSSL)