NIOSSL+GRPC.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 2025, 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. internal import GRPCCore
  17. package import GRPCNIOTransportCore
  18. internal import NIOCertificateReloading
  19. internal import NIOSSL
  20. @available(gRPCSwiftNIOTransport 2.0, *)
  21. extension NIOSSLSerializationFormats {
  22. fileprivate init(_ format: TLSConfig.SerializationFormat) {
  23. switch format.wrapped {
  24. case .pem:
  25. self = .pem
  26. case .der:
  27. self = .der
  28. }
  29. }
  30. }
  31. @available(gRPCSwiftNIOTransport 2.0, *)
  32. extension Sequence<TLSConfig.CertificateSource> {
  33. func sslCertificateSources() throws -> [NIOSSLCertificateSource] {
  34. var certificateSources: [NIOSSLCertificateSource] = []
  35. for source in self {
  36. switch source.wrapped {
  37. case .bytes(let bytes, let serializationFormat):
  38. switch serializationFormat.wrapped {
  39. case .der:
  40. certificateSources.append(
  41. .certificate(try NIOSSLCertificate(bytes: bytes, format: .der))
  42. )
  43. case .pem:
  44. let certificates = try NIOSSLCertificate.fromPEMBytes(bytes).map {
  45. NIOSSLCertificateSource.certificate($0)
  46. }
  47. certificateSources.append(contentsOf: certificates)
  48. }
  49. case .file(let path, let serializationFormat):
  50. switch serializationFormat.wrapped {
  51. case .der:
  52. try certificateSources.append(.certificate(NIOSSLCertificate.fromDERFile(path)))
  53. case .pem:
  54. let certificates = try NIOSSLCertificate.fromPEMFile(path).map {
  55. NIOSSLCertificateSource.certificate($0)
  56. }
  57. certificateSources.append(contentsOf: certificates)
  58. }
  59. case .transportSpecific(let specific):
  60. if let source = specific.wrapped as? NIOSSLCertificateSource {
  61. certificateSources.append(source)
  62. } else {
  63. fatalError("Invalid certificate source of type \(type(of: specific.wrapped))")
  64. }
  65. }
  66. }
  67. return certificateSources
  68. }
  69. }
  70. @available(gRPCSwiftNIOTransport 2.0, *)
  71. extension TLSConfig.PrivateKeySource {
  72. enum _NIOSSLPrivateKeySource: TransportSpecific {
  73. case customPrivateKey(any (NIOSSLCustomPrivateKey & Hashable))
  74. case privateKey(NIOSSLPrivateKeySource)
  75. }
  76. static func nioSSLSpecific(_ source: _NIOSSLPrivateKeySource) -> Self {
  77. .transportSpecific(source)
  78. }
  79. }
  80. @available(gRPCSwiftNIOTransport 2.0, *)
  81. extension NIOSSLPrivateKey {
  82. fileprivate static func makePrivateKey(
  83. from source: TLSConfig.PrivateKeySource
  84. ) throws -> NIOSSLPrivateKey {
  85. switch source.wrapped {
  86. case .file(let path, let serializationFormat):
  87. return try self.init(
  88. file: path,
  89. format: NIOSSLSerializationFormats(serializationFormat)
  90. )
  91. case .bytes(let bytes, let serializationFormat):
  92. return try self.init(
  93. bytes: bytes,
  94. format: NIOSSLSerializationFormats(serializationFormat)
  95. )
  96. case .transportSpecific(let extraSource):
  97. guard let source = extraSource as? TLSConfig.PrivateKeySource._NIOSSLPrivateKeySource else {
  98. fatalError("Invalid private key source of type \(type(of: extraSource))")
  99. }
  100. switch source {
  101. case .customPrivateKey(let privateKey):
  102. return self.init(customPrivateKey: privateKey)
  103. case .privateKey(.privateKey(let key)):
  104. return key
  105. case .privateKey(.file(let path)):
  106. switch path.split(separator: ".").last {
  107. case "pem":
  108. return try NIOSSLPrivateKey(file: path, format: .pem)
  109. case "der", "key":
  110. return try NIOSSLPrivateKey(file: path, format: .der)
  111. default:
  112. throw RPCError(
  113. code: .invalidArgument,
  114. message: "Couldn't load private key from \(path)."
  115. )
  116. }
  117. }
  118. }
  119. }
  120. }
  121. @available(gRPCSwiftNIOTransport 2.0, *)
  122. extension NIOSSLTrustRoots {
  123. fileprivate init(_ trustRoots: TLSConfig.TrustRootsSource) throws {
  124. switch trustRoots.wrapped {
  125. case .certificates(let certificateSources):
  126. var certificates: [NIOSSLCertificate] = []
  127. for source in certificateSources {
  128. switch source.wrapped {
  129. case .bytes(let bytes, let serializationFormat):
  130. switch serializationFormat.wrapped {
  131. case .pem:
  132. certificates.append(contentsOf: try NIOSSLCertificate.fromPEMBytes(bytes))
  133. case .der:
  134. certificates.append(try NIOSSLCertificate(bytes: bytes, format: .der))
  135. }
  136. case .file(let path, let serializationFormat):
  137. switch serializationFormat.wrapped {
  138. case .pem:
  139. certificates.append(contentsOf: try NIOSSLCertificate.fromPEMFile(path))
  140. case .der:
  141. certificates.append(try NIOSSLCertificate.fromDERFile(path))
  142. }
  143. case .transportSpecific(let specific):
  144. guard let source = specific.wrapped as? NIOSSLCertificateSource else {
  145. fatalError("Invalid certificate source of type \(type(of: specific.wrapped))")
  146. }
  147. switch source {
  148. case .certificate(let certificate):
  149. certificates.append(certificate)
  150. case .file(let path):
  151. switch path.split(separator: ".").last {
  152. case "pem":
  153. certificates.append(contentsOf: try NIOSSLCertificate.fromPEMFile(path))
  154. case "der":
  155. certificates.append(try NIOSSLCertificate.fromDERFile(path))
  156. default:
  157. throw RPCError(
  158. code: .invalidArgument,
  159. message: "Couldn't load certificate from \(path)."
  160. )
  161. }
  162. }
  163. }
  164. }
  165. self = .certificates(certificates)
  166. case .systemDefault:
  167. self = .default
  168. }
  169. }
  170. }
  171. @available(gRPCSwiftNIOTransport 2.0, *)
  172. extension CertificateVerification {
  173. fileprivate init(
  174. _ verificationMode: TLSConfig.CertificateVerification
  175. ) {
  176. switch verificationMode.wrapped {
  177. case .doNotVerify:
  178. self = .none
  179. case .fullVerification:
  180. self = .fullVerification
  181. case .noHostnameVerification:
  182. self = .noHostnameVerification
  183. }
  184. }
  185. }
  186. @available(gRPCSwiftNIOTransport 2.0, *)
  187. extension TLSConfiguration {
  188. package init(_ tlsConfig: HTTP2ServerTransport.Posix.TransportSecurity.TLS) throws {
  189. let certificateChain = try tlsConfig.certificateChain.sslCertificateSources()
  190. let privateKey = try NIOSSLPrivateKey.makePrivateKey(from: tlsConfig.privateKey)
  191. self = TLSConfiguration.makeServerConfiguration(
  192. certificateChain: certificateChain,
  193. privateKey: .privateKey(privateKey)
  194. )
  195. self.minimumTLSVersion = .tlsv12
  196. self.certificateVerification = CertificateVerification(tlsConfig.clientCertificateVerification)
  197. self.trustRoots = try NIOSSLTrustRoots(tlsConfig.trustRoots)
  198. self.applicationProtocols = ["grpc-exp", "h2"]
  199. if let reloader = tlsConfig.certificateReloader {
  200. self.setCertificateReloader(reloader)
  201. }
  202. }
  203. package init(_ tlsConfig: HTTP2ClientTransport.Posix.TransportSecurity.TLS) throws {
  204. self = TLSConfiguration.makeClientConfiguration()
  205. self.certificateChain = try tlsConfig.certificateChain.sslCertificateSources()
  206. if let privateKey = tlsConfig.privateKey {
  207. let privateKeySource = try NIOSSLPrivateKey.makePrivateKey(from: privateKey)
  208. self.privateKey = .privateKey(privateKeySource)
  209. }
  210. self.minimumTLSVersion = .tlsv12
  211. self.certificateVerification = CertificateVerification(tlsConfig.serverCertificateVerification)
  212. self.trustRoots = try NIOSSLTrustRoots(tlsConfig.trustRoots)
  213. self.applicationProtocols = ["grpc-exp", "h2"]
  214. if let reloader = tlsConfig.certificateReloader {
  215. self.setCertificateReloader(reloader)
  216. }
  217. }
  218. }