TLSEvaluationTests.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 IntermedateCA = 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.IntermedateCA)
  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 = [self.host: ServerTrustPolicy.PinCertificates(certificates: certificates, validateHost: true)]
  104. let manager = Manager(
  105. configuration: self.configuration,
  106. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  107. )
  108. let expectation = expectationWithDescription("\(self.URL)")
  109. var error: NSError?
  110. // When
  111. manager.request(.GET, self.URL)
  112. .response { _, _, _, responseError in
  113. error = responseError
  114. expectation.fulfill()
  115. }
  116. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  117. // Then
  118. XCTAssertNotNil(error, "error should not be nil")
  119. XCTAssertEqual(error?.code ?? -1, NSURLErrorCancelled, "error should be NSURLErrorCancelled")
  120. }
  121. func testThatExpiredCertificateRequestFailsWhenPinningAllCertificates() {
  122. // Given
  123. let certificates = [TestCertificates.Leaf, TestCertificates.IntermedateCA, TestCertificates.RootCA]
  124. let policies = [self.host: ServerTrustPolicy.PinCertificates(certificates: certificates, validateHost: true)]
  125. let manager = Manager(
  126. configuration: self.configuration,
  127. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  128. )
  129. let expectation = expectationWithDescription("\(self.URL)")
  130. var error: NSError?
  131. // When
  132. manager.request(.GET, self.URL)
  133. .response { _, _, _, responseError in
  134. error = responseError
  135. expectation.fulfill()
  136. }
  137. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  138. // Then
  139. XCTAssertNotNil(error, "error should not be nil")
  140. XCTAssertEqual(error?.code ?? -1, NSURLErrorCancelled, "error should be NSURLErrorCancelled")
  141. }
  142. // MARK: Server Trust Policy - Public Key Pinning Tests
  143. func testThatExpiredCertificateRequestFailsWhenPinningLeafPublicKeyWhileNotAllowingInvalidCertificates() {
  144. // Given
  145. let publicKeys = [TestPublicKeys.Leaf]
  146. let policies: [String: ServerTrustPolicy] = [
  147. self.host: .PinPublicKeys(publicKeys: publicKeys, validateHost: true, allowInvalidCertificates: false)
  148. ]
  149. let manager = Manager(
  150. configuration: self.configuration,
  151. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  152. )
  153. let expectation = expectationWithDescription("\(self.URL)")
  154. var error: NSError?
  155. // When
  156. manager.request(.GET, self.URL)
  157. .response { _, _, _, responseError in
  158. error = responseError
  159. expectation.fulfill()
  160. }
  161. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  162. // Then
  163. XCTAssertNotNil(error, "error should not be nil")
  164. XCTAssertEqual(error?.code ?? -1, NSURLErrorCancelled, "error should be NSURLErrorCancelled")
  165. }
  166. func testThatExpiredCertificateRequestSucceedsWhenPinningLeafPublicKeyAndAllowingInvalidCertificates() {
  167. // Given
  168. let publicKeys = [TestPublicKeys.Leaf]
  169. let policies: [String: ServerTrustPolicy] = [
  170. self.host: .PinPublicKeys(publicKeys: publicKeys, validateHost: true, allowInvalidCertificates: true)
  171. ]
  172. let manager = Manager(
  173. configuration: self.configuration,
  174. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  175. )
  176. let expectation = expectationWithDescription("\(self.URL)")
  177. var error: NSError?
  178. // When
  179. manager.request(.GET, self.URL)
  180. .response { _, _, _, responseError in
  181. error = responseError
  182. expectation.fulfill()
  183. }
  184. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  185. // Then
  186. XCTAssertNil(error, "error should be nil")
  187. }
  188. func testThatExpiredCertificateRequestSucceedsWhenPinningIntermediateCAPublicKeyAndAllowingInvalidCertificates() {
  189. // Given
  190. let publicKeys = [TestPublicKeys.IntermediateCA]
  191. let policies: [String: ServerTrustPolicy] = [
  192. self.host: .PinPublicKeys(publicKeys: publicKeys, validateHost: true, allowInvalidCertificates: true)
  193. ]
  194. let manager = Manager(
  195. configuration: self.configuration,
  196. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  197. )
  198. let expectation = expectationWithDescription("\(self.URL)")
  199. var error: NSError?
  200. // When
  201. manager.request(.GET, self.URL)
  202. .response { _, _, _, responseError in
  203. error = responseError
  204. expectation.fulfill()
  205. }
  206. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  207. // Then
  208. XCTAssertNil(error, "error should be nil")
  209. }
  210. func testThatExpiredCertificateRequestSucceedsWhenPinningRootCAPublicKeyAndAllowingInvalidCertificates() {
  211. // Given
  212. let publicKeys = [TestPublicKeys.RootCA]
  213. let policies: [String: ServerTrustPolicy] = [
  214. self.host: .PinPublicKeys(publicKeys: publicKeys, validateHost: true, allowInvalidCertificates: true)
  215. ]
  216. let manager = Manager(
  217. configuration: self.configuration,
  218. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  219. )
  220. let expectation = expectationWithDescription("\(self.URL)")
  221. var error: NSError?
  222. // When
  223. manager.request(.GET, self.URL)
  224. .response { _, _, _, responseError in
  225. error = responseError
  226. expectation.fulfill()
  227. }
  228. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  229. // Then
  230. XCTAssertNil(error, "error should be nil")
  231. }
  232. // MARK: Server Trust Policy - Disabling Evaluation Tests
  233. func testThatExpiredCertificateRequestSucceedsWhenDisablingEvaluation() {
  234. // Given
  235. let policies = [self.host: ServerTrustPolicy.DisableEvaluation]
  236. let manager = Manager(
  237. configuration: self.configuration,
  238. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  239. )
  240. let expectation = expectationWithDescription("\(self.URL)")
  241. var error: NSError?
  242. // When
  243. manager.request(.GET, self.URL)
  244. .response { _, _, _, responseError in
  245. error = responseError
  246. expectation.fulfill()
  247. }
  248. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  249. // Then
  250. XCTAssertNil(error, "error should be nil")
  251. }
  252. // MARK: Server Trust Policy - Custom Evaluation Tests
  253. func testThatExpiredCertificateRequestSucceedsWhenCustomEvaluationReturnsTrue() {
  254. // Given
  255. let policies = [
  256. self.host: ServerTrustPolicy.CustomEvaluation { _, _ in
  257. // Implement a custom evaluation routine here...
  258. return true
  259. }
  260. ]
  261. let manager = Manager(
  262. configuration: self.configuration,
  263. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  264. )
  265. let expectation = expectationWithDescription("\(self.URL)")
  266. var error: NSError?
  267. // When
  268. manager.request(.GET, self.URL)
  269. .response { _, _, _, responseError in
  270. error = responseError
  271. expectation.fulfill()
  272. }
  273. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  274. // Then
  275. XCTAssertNil(error, "error should be nil")
  276. }
  277. func testThatExpiredCertificateRequestFailsWhenCustomEvaluationReturnsFalse() {
  278. // Given
  279. let policies = [
  280. self.host: ServerTrustPolicy.CustomEvaluation { _, _ in
  281. // Implement a custom evaluation routine here...
  282. return false
  283. }
  284. ]
  285. let manager = Manager(
  286. configuration: self.configuration,
  287. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  288. )
  289. let expectation = expectationWithDescription("\(self.URL)")
  290. var error: NSError?
  291. // When
  292. manager.request(.GET, self.URL)
  293. .response { _, _, _, responseError in
  294. error = responseError
  295. expectation.fulfill()
  296. }
  297. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  298. // Then
  299. XCTAssertNotNil(error, "error should not be nil")
  300. XCTAssertEqual(error?.code ?? -1, NSURLErrorCancelled, "error should be NSURLErrorCancelled")
  301. }
  302. }