TLSEvaluationTests.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // DownloadTests.swift
  2. //
  3. // Copyright (c) 2014–2015 Alamofire Software Foundation (http://alamofire.org/)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. import Alamofire
  23. import Foundation
  24. import XCTest
  25. private struct TestCertificates {
  26. static let RootCA = TestCertificates.certificateWithFileName("root-ca-disig")
  27. static let IntermediateCA = TestCertificates.certificateWithFileName("intermediate-ca-disig")
  28. static let Leaf = TestCertificates.certificateWithFileName("testssl-expire.disig.sk")
  29. static func certificateWithFileName(fileName: String) -> SecCertificate {
  30. class Bundle {}
  31. let filePath = NSBundle(forClass: Bundle.self).pathForResource(fileName, ofType: "cer")!
  32. let data = NSData(contentsOfFile: filePath)!
  33. let certificate = SecCertificateCreateWithData(nil, data).takeRetainedValue()
  34. return certificate
  35. }
  36. }
  37. // MARK: -
  38. private struct TestPublicKeys {
  39. static let RootCA = TestPublicKeys.publicKeyForCertificate(TestCertificates.RootCA)
  40. static let IntermediateCA = TestPublicKeys.publicKeyForCertificate(TestCertificates.IntermediateCA)
  41. static let Leaf = TestPublicKeys.publicKeyForCertificate(TestCertificates.Leaf)
  42. static func publicKeyForCertificate(certificate: SecCertificate) -> SecKey {
  43. let policy = SecPolicyCreateBasicX509().takeRetainedValue()
  44. var unmanagedTrust: Unmanaged<SecTrust>?
  45. let trustCreationStatus = SecTrustCreateWithCertificates(certificate, policy, &unmanagedTrust)
  46. let trust = unmanagedTrust!.takeRetainedValue()
  47. let publicKey = SecTrustCopyPublicKey(trust).takeRetainedValue()
  48. return publicKey
  49. }
  50. }
  51. // MARK: -
  52. class TLSEvaluationExpiredLeafCertificateTestCase: BaseTestCase {
  53. let URL = "https://testssl-expire.disig.sk/"
  54. let host = "testssl-expire.disig.sk"
  55. var configuration: NSURLSessionConfiguration!
  56. // MARK: Setup and Teardown
  57. override func setUp() {
  58. super.setUp()
  59. self.configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
  60. }
  61. // MARK: Default Behavior Tests
  62. func testThatExpiredCertificateRequestFailsWithNoServerTrustPolicy() {
  63. // Given
  64. let expectation = expectationWithDescription("\(self.URL)")
  65. let manager = Manager(configuration: self.configuration)
  66. var error: NSError?
  67. // When
  68. manager.request(.GET, self.URL)
  69. .response { _, _, _, responseError in
  70. error = responseError
  71. expectation.fulfill()
  72. }
  73. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  74. // Then
  75. XCTAssertNotNil(error, "error should not be nil")
  76. XCTAssertEqual(error?.code ?? -1, NSURLErrorServerCertificateUntrusted, "error should be NSURLErrorServerCertificateUntrusted")
  77. }
  78. // MARK: Server Trust Policy - Perform Default Tests
  79. func testThatExpiredCertificateRequestFailsWithDefaultServerTrustPolicy() {
  80. // Given
  81. let policies = [self.host: ServerTrustPolicy.PerformDefaultEvaluation(validateHost: true)]
  82. let manager = Manager(
  83. configuration: self.configuration,
  84. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  85. )
  86. let expectation = expectationWithDescription("\(self.URL)")
  87. var error: NSError?
  88. // When
  89. manager.request(.GET, self.URL)
  90. .response { _, _, _, responseError in
  91. error = responseError
  92. expectation.fulfill()
  93. }
  94. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  95. // Then
  96. XCTAssertNotNil(error, "error should not be nil")
  97. XCTAssertEqual(error?.code ?? -1, NSURLErrorCancelled, "error should be NSURLErrorCancelled")
  98. }
  99. // MARK: Server Trust Policy - Certificate Pinning Tests
  100. func testThatExpiredCertificateRequestFailsWhenPinningLeafCertificate() {
  101. // Given
  102. let certificates = [TestCertificates.Leaf]
  103. let policies: [String: ServerTrustPolicy] = [
  104. self.host: .PinCertificates(certificates: certificates, validateCertificateChain: true, validateHost: true)
  105. ]
  106. let manager = Manager(
  107. configuration: self.configuration,
  108. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  109. )
  110. let expectation = expectationWithDescription("\(self.URL)")
  111. var error: NSError?
  112. // When
  113. manager.request(.GET, self.URL)
  114. .response { _, _, _, responseError in
  115. error = responseError
  116. expectation.fulfill()
  117. }
  118. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  119. // Then
  120. XCTAssertNotNil(error, "error should not be nil")
  121. XCTAssertEqual(error?.code ?? -1, NSURLErrorCancelled, "error should be NSURLErrorCancelled")
  122. }
  123. func testThatExpiredCertificateRequestFailsWhenPinningAllCertificates() {
  124. // Given
  125. let certificates = [TestCertificates.Leaf, TestCertificates.IntermediateCA, TestCertificates.RootCA]
  126. let policies: [String: ServerTrustPolicy] = [
  127. self.host: .PinCertificates(certificates: certificates, validateCertificateChain: true, validateHost: true)
  128. ]
  129. let manager = Manager(
  130. configuration: self.configuration,
  131. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  132. )
  133. let expectation = expectationWithDescription("\(self.URL)")
  134. var error: NSError?
  135. // When
  136. manager.request(.GET, self.URL)
  137. .response { _, _, _, responseError in
  138. error = responseError
  139. expectation.fulfill()
  140. }
  141. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  142. // Then
  143. XCTAssertNotNil(error, "error should not be nil")
  144. XCTAssertEqual(error?.code ?? -1, NSURLErrorCancelled, "error should be NSURLErrorCancelled")
  145. }
  146. // MARK: Server Trust Policy - Public Key Pinning Tests
  147. func testThatExpiredCertificateRequestFailsWhenPinningLeafPublicKeyWithCertificateChainValidation() {
  148. // Given
  149. let publicKeys = [TestPublicKeys.Leaf]
  150. let policies: [String: ServerTrustPolicy] = [
  151. self.host: .PinPublicKeys(publicKeys: publicKeys, validateCertificateChain: true, validateHost: true)
  152. ]
  153. let manager = Manager(
  154. configuration: self.configuration,
  155. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  156. )
  157. let expectation = expectationWithDescription("\(self.URL)")
  158. var error: NSError?
  159. // When
  160. manager.request(.GET, self.URL)
  161. .response { _, _, _, responseError in
  162. error = responseError
  163. expectation.fulfill()
  164. }
  165. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  166. // Then
  167. XCTAssertNotNil(error, "error should not be nil")
  168. XCTAssertEqual(error?.code ?? -1, NSURLErrorCancelled, "error should be NSURLErrorCancelled")
  169. }
  170. func testThatExpiredCertificateRequestSucceedsWhenPinningLeafPublicKeyWithoutCertificateChainValidation() {
  171. // Given
  172. let publicKeys = [TestPublicKeys.Leaf]
  173. let policies: [String: ServerTrustPolicy] = [
  174. self.host: .PinPublicKeys(publicKeys: publicKeys, validateCertificateChain: false, validateHost: true)
  175. ]
  176. let manager = Manager(
  177. configuration: self.configuration,
  178. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  179. )
  180. let expectation = expectationWithDescription("\(self.URL)")
  181. var error: NSError?
  182. // When
  183. manager.request(.GET, self.URL)
  184. .response { _, _, _, responseError in
  185. error = responseError
  186. expectation.fulfill()
  187. }
  188. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  189. // Then
  190. XCTAssertNil(error, "error should be nil")
  191. }
  192. func testThatExpiredCertificateRequestSucceedsWhenPinningIntermediateCAPublicKeyWithoutCertificateChainValidation() {
  193. // Given
  194. let publicKeys = [TestPublicKeys.IntermediateCA]
  195. let policies: [String: ServerTrustPolicy] = [
  196. self.host: .PinPublicKeys(publicKeys: publicKeys, validateCertificateChain: false, validateHost: true)
  197. ]
  198. let manager = Manager(
  199. configuration: self.configuration,
  200. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  201. )
  202. let expectation = expectationWithDescription("\(self.URL)")
  203. var error: NSError?
  204. // When
  205. manager.request(.GET, self.URL)
  206. .response { _, _, _, responseError in
  207. error = responseError
  208. expectation.fulfill()
  209. }
  210. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  211. // Then
  212. XCTAssertNil(error, "error should be nil")
  213. }
  214. func testThatExpiredCertificateRequestSucceedsWhenPinningRootCAPublicKeyWithoutCertificateChainValidation() {
  215. // Given
  216. let publicKeys = [TestPublicKeys.RootCA]
  217. let policies: [String: ServerTrustPolicy] = [
  218. self.host: .PinPublicKeys(publicKeys: publicKeys, validateCertificateChain: false, validateHost: true)
  219. ]
  220. let manager = Manager(
  221. configuration: self.configuration,
  222. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  223. )
  224. let expectation = expectationWithDescription("\(self.URL)")
  225. var error: NSError?
  226. // When
  227. manager.request(.GET, self.URL)
  228. .response { _, _, _, responseError in
  229. error = responseError
  230. expectation.fulfill()
  231. }
  232. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  233. // Then
  234. XCTAssertNil(error, "error should be nil")
  235. }
  236. // MARK: Server Trust Policy - Disabling Evaluation Tests
  237. func testThatExpiredCertificateRequestSucceedsWhenDisablingEvaluation() {
  238. // Given
  239. let policies = [self.host: ServerTrustPolicy.DisableEvaluation]
  240. let manager = Manager(
  241. configuration: self.configuration,
  242. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  243. )
  244. let expectation = expectationWithDescription("\(self.URL)")
  245. var error: NSError?
  246. // When
  247. manager.request(.GET, self.URL)
  248. .response { _, _, _, responseError in
  249. error = responseError
  250. expectation.fulfill()
  251. }
  252. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  253. // Then
  254. XCTAssertNil(error, "error should be nil")
  255. }
  256. // MARK: Server Trust Policy - Custom Evaluation Tests
  257. func testThatExpiredCertificateRequestSucceedsWhenCustomEvaluationReturnsTrue() {
  258. // Given
  259. let policies = [
  260. self.host: ServerTrustPolicy.CustomEvaluation { _, _ in
  261. // Implement a custom evaluation routine here...
  262. return true
  263. }
  264. ]
  265. let manager = Manager(
  266. configuration: self.configuration,
  267. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  268. )
  269. let expectation = expectationWithDescription("\(self.URL)")
  270. var error: NSError?
  271. // When
  272. manager.request(.GET, self.URL)
  273. .response { _, _, _, responseError in
  274. error = responseError
  275. expectation.fulfill()
  276. }
  277. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  278. // Then
  279. XCTAssertNil(error, "error should be nil")
  280. }
  281. func testThatExpiredCertificateRequestFailsWhenCustomEvaluationReturnsFalse() {
  282. // Given
  283. let policies = [
  284. self.host: ServerTrustPolicy.CustomEvaluation { _, _ in
  285. // Implement a custom evaluation routine here...
  286. return false
  287. }
  288. ]
  289. let manager = Manager(
  290. configuration: self.configuration,
  291. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  292. )
  293. let expectation = expectationWithDescription("\(self.URL)")
  294. var error: NSError?
  295. // When
  296. manager.request(.GET, self.URL)
  297. .response { _, _, _, responseError in
  298. error = responseError
  299. expectation.fulfill()
  300. }
  301. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  302. // Then
  303. XCTAssertNotNil(error, "error should not be nil")
  304. XCTAssertEqual(error?.code ?? -1, NSURLErrorCancelled, "error should be NSURLErrorCancelled")
  305. }
  306. }