TLSVersion.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2022, 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 NIOCore
  17. #if canImport(NIOSSL)
  18. import NIOSSL
  19. #endif
  20. #if canImport(Network)
  21. import Network
  22. import NIOTransportServices
  23. #endif
  24. // The same as 'TLSVersion' which is defined in NIOSSL which we don't always have.
  25. enum GRPCTLSVersion: Hashable {
  26. case tlsv1
  27. case tlsv11
  28. case tlsv12
  29. case tlsv13
  30. }
  31. #if canImport(NIOSSL)
  32. extension GRPCTLSVersion {
  33. init(_ tlsVersion: TLSVersion) {
  34. switch tlsVersion {
  35. case .tlsv1:
  36. self = .tlsv1
  37. case .tlsv11:
  38. self = .tlsv11
  39. case .tlsv12:
  40. self = .tlsv12
  41. case .tlsv13:
  42. self = .tlsv13
  43. }
  44. }
  45. }
  46. #endif
  47. #if canImport(Network)
  48. @available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
  49. extension GRPCTLSVersion {
  50. init?(_ metadata: NWProtocolTLS.Metadata) {
  51. let protocolMetadata = metadata.securityProtocolMetadata
  52. if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 7.0, *) {
  53. let nwTLSVersion = sec_protocol_metadata_get_negotiated_tls_protocol_version(protocolMetadata)
  54. switch nwTLSVersion {
  55. case .TLSv10:
  56. self = .tlsv1
  57. case .TLSv11:
  58. self = .tlsv11
  59. case .TLSv12:
  60. self = .tlsv12
  61. case .TLSv13:
  62. self = .tlsv13
  63. case .DTLSv10, .DTLSv12:
  64. return nil
  65. @unknown default:
  66. return nil
  67. }
  68. } else {
  69. let sslVersion = sec_protocol_metadata_get_negotiated_protocol_version(protocolMetadata)
  70. switch sslVersion {
  71. case .sslProtocolUnknown:
  72. return nil
  73. case .tlsProtocol1, .tlsProtocol1Only:
  74. self = .tlsv1
  75. case .tlsProtocol11:
  76. self = .tlsv11
  77. case .tlsProtocol12:
  78. self = .tlsv12
  79. case .tlsProtocol13:
  80. self = .tlsv13
  81. case .dtlsProtocol1,
  82. .dtlsProtocol12,
  83. .sslProtocol2,
  84. .sslProtocol3,
  85. .sslProtocol3Only,
  86. .sslProtocolAll,
  87. .tlsProtocolMaxSupported:
  88. return nil
  89. @unknown default:
  90. return nil
  91. }
  92. }
  93. }
  94. }
  95. #endif
  96. extension Channel {
  97. /// This method tries to get the TLS version from either the Network.framework or NIOSSL
  98. /// - Precondition: Must be called on the `EventLoop` the `Channel` is running on.
  99. func getTLSVersionSync(
  100. file: StaticString = #fileID,
  101. line: UInt = #line
  102. ) throws -> GRPCTLSVersion? {
  103. #if canImport(Network)
  104. if #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
  105. do {
  106. // cast can never fail because we explicitly ask for the NWProtocolTLS Metadata.
  107. // it may still be nil if Network.framework isn't used for TLS in which case we will
  108. // fall through and try to get the TLS version from NIOSSL
  109. if let metadata = try self.getMetadataSync(
  110. definition: NWProtocolTLS.definition,
  111. file: file,
  112. line: line
  113. ) as! NWProtocolTLS.Metadata? {
  114. return GRPCTLSVersion(metadata)
  115. }
  116. } catch is NIOTSChannelIsNotANIOTSConnectionChannel {
  117. // Not a NIOTS channel, we might be using NIOSSL so try that next.
  118. }
  119. }
  120. #endif
  121. #if canImport(NIOSSL)
  122. return try self.pipeline.syncOperations.nioSSL_tlsVersion().map(GRPCTLSVersion.init)
  123. #else
  124. return nil
  125. #endif
  126. }
  127. }