TLSConfig.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2024, 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. @available(gRPCSwiftNIOTransport 1.0, *)
  17. public enum TLSConfig: Sendable {
  18. /// The serialization format of the provided certificates and private keys.
  19. public struct SerializationFormat: Sendable, Equatable {
  20. package enum Wrapped {
  21. case pem
  22. case der
  23. }
  24. package let wrapped: Wrapped
  25. public static let pem = Self(wrapped: .pem)
  26. public static let der = Self(wrapped: .der)
  27. }
  28. /// A description of where a certificate is coming from: either a byte array or a file.
  29. /// The serialization format is specified by ``TLSConfig/SerializationFormat``.
  30. public struct CertificateSource: Sendable, Equatable {
  31. package enum Wrapped: Equatable {
  32. case file(path: String, format: SerializationFormat)
  33. case bytes(bytes: [UInt8], format: SerializationFormat)
  34. case transportSpecific(TransportSpecific)
  35. }
  36. package let wrapped: Wrapped
  37. /// The certificate's source is a file.
  38. /// - Parameters:
  39. /// - path: The file path containing the certificate.
  40. /// - format: The certificate's format, as a ``TLSConfig/SerializationFormat``.
  41. /// - Returns: A source describing the certificate source is the given file.
  42. public static func file(path: String, format: SerializationFormat) -> Self {
  43. Self(wrapped: .file(path: path, format: format))
  44. }
  45. /// The certificate's source is an array of bytes.
  46. /// - Parameters:
  47. /// - bytes: The array of bytes making up the certificate.
  48. /// - format: The certificate's format, as a ``TLSConfig/SerializationFormat``.
  49. /// - Returns: A source describing the certificate source is the given bytes.
  50. public static func bytes(_ bytes: [UInt8], format: SerializationFormat) -> Self {
  51. Self(wrapped: .bytes(bytes: bytes, format: format))
  52. }
  53. }
  54. /// A description of where the private key is coming from: either a byte array or a file.
  55. /// The serialization format is specified by ``TLSConfig/SerializationFormat``.
  56. public struct PrivateKeySource: Sendable {
  57. package enum Wrapped {
  58. case file(path: String, format: SerializationFormat)
  59. case bytes(bytes: [UInt8], format: SerializationFormat)
  60. case transportSpecific(any TransportSpecific)
  61. }
  62. package let wrapped: Wrapped
  63. /// The private key's source is a file.
  64. /// - Parameters:
  65. /// - path: The file path containing the private key.
  66. /// - format: The private key's format, as a ``TLSConfig/SerializationFormat``.
  67. /// - Returns: A source describing the private key source is the given file.
  68. public static func file(path: String, format: SerializationFormat) -> Self {
  69. Self(wrapped: .file(path: path, format: format))
  70. }
  71. /// The private key's source is an array of bytes.
  72. /// - Parameters:
  73. /// - bytes: The array of bytes making up the private key.
  74. /// - format: The private key's format, as a ``TLSConfig/SerializationFormat``.
  75. /// - Returns: A source describing the private key source is the given bytes.
  76. public static func bytes(
  77. _ bytes: [UInt8],
  78. format: SerializationFormat
  79. ) -> Self {
  80. Self(wrapped: .bytes(bytes: bytes, format: format))
  81. }
  82. }
  83. /// A description of where the trust roots are coming from: either a custom certificate chain, or the system default trust store.
  84. public struct TrustRootsSource: Sendable, Equatable {
  85. package enum Wrapped: Equatable {
  86. case certificates([CertificateSource])
  87. case systemDefault
  88. }
  89. package let wrapped: Wrapped
  90. /// A list of ``TLSConfig/CertificateSource``s making up the
  91. /// chain of trust.
  92. /// - Parameter certificateSources: The sources for the certificates that make up the chain of trust.
  93. /// - Returns: A trust root for the given chain of trust.
  94. public static func certificates(
  95. _ certificateSources: [CertificateSource]
  96. ) -> Self {
  97. Self(wrapped: .certificates(certificateSources))
  98. }
  99. /// The system default trust store.
  100. public static let systemDefault: Self = Self(wrapped: .systemDefault)
  101. }
  102. /// How to verify certificates.
  103. public struct CertificateVerification: Sendable, Equatable {
  104. package enum Wrapped: Equatable {
  105. case doNotVerify
  106. case fullVerification
  107. case noHostnameVerification
  108. }
  109. package let wrapped: Wrapped
  110. /// All certificate verification disabled.
  111. public static let noVerification: Self = Self(wrapped: .doNotVerify)
  112. /// Certificates will be validated against the trust store, but will not be checked to see if they are valid for the given hostname.
  113. public static let noHostnameVerification: Self = Self(wrapped: .noHostnameVerification)
  114. /// Certificates will be validated against the trust store and checked against the hostname of the service we are contacting.
  115. public static let fullVerification: Self = Self(wrapped: .fullVerification)
  116. }
  117. }