TLSConfig.swift 5.2 KB

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