TLSConfig.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. case transportSpecific(any TransportSpecific)
  59. }
  60. package let wrapped: Wrapped
  61. /// The private key's source is a file.
  62. /// - Parameters:
  63. /// - path: The file path containing the private key.
  64. /// - format: The private key's format, as a ``TLSConfig/SerializationFormat``.
  65. /// - Returns: A source describing the private key source is the given file.
  66. public static func file(path: String, format: SerializationFormat) -> Self {
  67. Self(wrapped: .file(path: path, format: format))
  68. }
  69. /// The private key's source is an array of bytes.
  70. /// - Parameters:
  71. /// - bytes: The array of bytes making up the private key.
  72. /// - format: The private key's format, as a ``TLSConfig/SerializationFormat``.
  73. /// - Returns: A source describing the private key source is the given bytes.
  74. public static func bytes(
  75. _ bytes: [UInt8],
  76. format: SerializationFormat
  77. ) -> Self {
  78. Self(wrapped: .bytes(bytes: bytes, format: format))
  79. }
  80. }
  81. /// A description of where the trust roots are coming from: either a custom certificate chain, or the system default trust store.
  82. public struct TrustRootsSource: Sendable, Equatable {
  83. package enum Wrapped: Equatable {
  84. case certificates([CertificateSource])
  85. case systemDefault
  86. }
  87. package let wrapped: Wrapped
  88. /// A list of ``TLSConfig/CertificateSource``s making up the
  89. /// chain of trust.
  90. /// - Parameter certificateSources: The sources for the certificates that make up the chain of trust.
  91. /// - Returns: A trust root for the given chain of trust.
  92. public static func certificates(
  93. _ certificateSources: [CertificateSource]
  94. ) -> Self {
  95. Self(wrapped: .certificates(certificateSources))
  96. }
  97. /// The system default trust store.
  98. public static let systemDefault: Self = Self(wrapped: .systemDefault)
  99. }
  100. /// How to verify certificates.
  101. public struct CertificateVerification: Sendable, Equatable {
  102. package enum Wrapped: Equatable {
  103. case doNotVerify
  104. case fullVerification
  105. case noHostnameVerification
  106. }
  107. package let wrapped: Wrapped
  108. /// All certificate verification disabled.
  109. public static let noVerification: Self = Self(wrapped: .doNotVerify)
  110. /// Certificates will be validated against the trust store, but will not be checked to see if they are valid for the given hostname.
  111. public static let noHostnameVerification: Self = Self(wrapped: .noHostnameVerification)
  112. /// Certificates will be validated against the trust store and checked against the hostname of the service we are contacting.
  113. public static let fullVerification: Self = Self(wrapped: .fullVerification)
  114. }
  115. }