You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.8 KiB

5 years ago
  1. //
  2. // SSLSecurity.swift
  3. // SocketIO-iOS
  4. //
  5. // Created by Lukas Schmidt on 24.09.17.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. import Foundation
  25. import Starscream
  26. /// A wrapper around Starscream's SSLSecurity that provides a minimal Objective-C interface.
  27. open class SSLSecurity : NSObject {
  28. // MARK: Properties
  29. /// The internal Starscream SSLSecurity.
  30. public let security: Starscream.SSLSecurity
  31. init(security: Starscream.SSLSecurity) {
  32. self.security = security
  33. }
  34. // MARK: Methods
  35. /// Creates a new SSLSecurity that specifies whether to use publicKeys or certificates should be used for SSL
  36. /// pinning validation
  37. ///
  38. /// - parameter usePublicKeys: is to specific if the publicKeys or certificates should be used for SSL pinning
  39. /// validation
  40. @objc
  41. public convenience init(usePublicKeys: Bool = true) {
  42. let security = Starscream.SSLSecurity(usePublicKeys: usePublicKeys)
  43. self.init(security: security)
  44. }
  45. /// Designated init
  46. ///
  47. /// - parameter certs: is the certificates or public keys to use
  48. /// - parameter usePublicKeys: is to specific if the publicKeys or certificates should be used for SSL pinning
  49. /// validation
  50. /// - returns: a representation security object to be used with
  51. public convenience init(certs: [SSLCert], usePublicKeys: Bool) {
  52. let security = Starscream.SSLSecurity(certs: certs, usePublicKeys: usePublicKeys)
  53. self.init(security: security)
  54. }
  55. /// Returns whether or not the given trust is valid.
  56. ///
  57. /// - parameter trust: The trust to validate.
  58. /// - parameter domain: The CN domain to validate.
  59. /// - returns: Whether or not this is valid.
  60. public func isValid(_ trust: SecTrust, domain: String?) -> Bool {
  61. return security.isValid(trust, domain: domain)
  62. }
  63. }