TLSEvaluationTests.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. // TLSEvaluationTests.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. configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
  60. }
  61. // MARK: Default Behavior Tests
  62. func testThatExpiredCertificateRequestFailsWithNoServerTrustPolicy() {
  63. // Given
  64. let expectation = expectationWithDescription("\(URL)")
  65. let manager = Manager(configuration: configuration)
  66. var error: NSError?
  67. // When
  68. manager.request(.GET, URL)
  69. .response { _, _, _, responseError in
  70. error = responseError
  71. expectation.fulfill()
  72. }
  73. waitForExpectationsWithTimeout(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 = [host: ServerTrustPolicy.PerformDefaultEvaluation(validateHost: true)]
  82. let manager = Manager(
  83. configuration: configuration,
  84. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  85. )
  86. let expectation = expectationWithDescription("\(URL)")
  87. var error: NSError?
  88. // When
  89. manager.request(.GET, URL)
  90. .response { _, _, _, responseError in
  91. error = responseError
  92. expectation.fulfill()
  93. }
  94. waitForExpectationsWithTimeout(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 testThatExpiredCertificateRequestFailsWhenPinningLeafCertificateWithCertificateChainValidation() {
  101. // Given
  102. let certificates = [TestCertificates.Leaf]
  103. let policies: [String: ServerTrustPolicy] = [
  104. host: .PinCertificates(certificates: certificates, validateCertificateChain: true, validateHost: true)
  105. ]
  106. let manager = Manager(
  107. configuration: configuration,
  108. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  109. )
  110. let expectation = expectationWithDescription("\(URL)")
  111. var error: NSError?
  112. // When
  113. manager.request(.GET, URL)
  114. .response { _, _, _, responseError in
  115. error = responseError
  116. expectation.fulfill()
  117. }
  118. waitForExpectationsWithTimeout(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 testThatExpiredCertificateRequestFailsWhenPinningAllCertificatesWithCertificateChainValidation() {
  124. // Given
  125. let certificates = [TestCertificates.Leaf, TestCertificates.IntermediateCA, TestCertificates.RootCA]
  126. let policies: [String: ServerTrustPolicy] = [
  127. host: .PinCertificates(certificates: certificates, validateCertificateChain: true, validateHost: true)
  128. ]
  129. let manager = Manager(
  130. configuration: configuration,
  131. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  132. )
  133. let expectation = expectationWithDescription("\(URL)")
  134. var error: NSError?
  135. // When
  136. manager.request(.GET, URL)
  137. .response { _, _, _, responseError in
  138. error = responseError
  139. expectation.fulfill()
  140. }
  141. waitForExpectationsWithTimeout(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. func testThatExpiredCertificateRequestSucceedsWhenPinningLeafCertificateWithoutCertificateChainValidation() {
  147. // Given
  148. let certificates = [TestCertificates.Leaf]
  149. let policies: [String: ServerTrustPolicy] = [
  150. host: .PinCertificates(certificates: certificates, validateCertificateChain: false, validateHost: true)
  151. ]
  152. let manager = Manager(
  153. configuration: configuration,
  154. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  155. )
  156. let expectation = expectationWithDescription("\(URL)")
  157. var error: NSError?
  158. // When
  159. manager.request(.GET, URL)
  160. .response { _, _, _, responseError in
  161. error = responseError
  162. expectation.fulfill()
  163. }
  164. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  165. // Then
  166. XCTAssertNil(error, "error should be nil")
  167. }
  168. func testThatExpiredCertificateRequestSucceedsWhenPinningIntermediateCACertificateWithoutCertificateChainValidation() {
  169. // Given
  170. let certificates = [TestCertificates.IntermediateCA]
  171. let policies: [String: ServerTrustPolicy] = [
  172. host: .PinCertificates(certificates: certificates, validateCertificateChain: false, validateHost: true)
  173. ]
  174. let manager = Manager(
  175. configuration: configuration,
  176. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  177. )
  178. let expectation = expectationWithDescription("\(URL)")
  179. var error: NSError?
  180. // When
  181. manager.request(.GET, URL)
  182. .response { _, _, _, responseError in
  183. error = responseError
  184. expectation.fulfill()
  185. }
  186. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  187. // Then
  188. XCTAssertNil(error, "error should be nil")
  189. }
  190. func testThatExpiredCertificateRequestSucceedsWhenPinningRootCACertificateWithoutCertificateChainValidation() {
  191. // Given
  192. let certificates = [TestCertificates.RootCA]
  193. let policies: [String: ServerTrustPolicy] = [
  194. host: .PinCertificates(certificates: certificates, validateCertificateChain: false, validateHost: true)
  195. ]
  196. let manager = Manager(
  197. configuration: configuration,
  198. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  199. )
  200. let expectation = expectationWithDescription("\(URL)")
  201. var error: NSError?
  202. // When
  203. manager.request(.GET, URL)
  204. .response { _, _, _, responseError in
  205. error = responseError
  206. expectation.fulfill()
  207. }
  208. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  209. // Then
  210. XCTAssertNil(error, "error should be nil")
  211. }
  212. // MARK: Server Trust Policy - Public Key Pinning Tests
  213. func testThatExpiredCertificateRequestFailsWhenPinningLeafPublicKeyWithCertificateChainValidation() {
  214. // Given
  215. let publicKeys = [TestPublicKeys.Leaf]
  216. let policies: [String: ServerTrustPolicy] = [
  217. host: .PinPublicKeys(publicKeys: publicKeys, validateCertificateChain: true, validateHost: true)
  218. ]
  219. let manager = Manager(
  220. configuration: configuration,
  221. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  222. )
  223. let expectation = expectationWithDescription("\(URL)")
  224. var error: NSError?
  225. // When
  226. manager.request(.GET, URL)
  227. .response { _, _, _, responseError in
  228. error = responseError
  229. expectation.fulfill()
  230. }
  231. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  232. // Then
  233. XCTAssertNotNil(error, "error should not be nil")
  234. XCTAssertEqual(error?.code ?? -1, NSURLErrorCancelled, "error should be NSURLErrorCancelled")
  235. }
  236. func testThatExpiredCertificateRequestSucceedsWhenPinningLeafPublicKeyWithoutCertificateChainValidation() {
  237. // Given
  238. let publicKeys = [TestPublicKeys.Leaf]
  239. let policies: [String: ServerTrustPolicy] = [
  240. host: .PinPublicKeys(publicKeys: publicKeys, validateCertificateChain: false, validateHost: true)
  241. ]
  242. let manager = Manager(
  243. configuration: configuration,
  244. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  245. )
  246. let expectation = expectationWithDescription("\(URL)")
  247. var error: NSError?
  248. // When
  249. manager.request(.GET, URL)
  250. .response { _, _, _, responseError in
  251. error = responseError
  252. expectation.fulfill()
  253. }
  254. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  255. // Then
  256. XCTAssertNil(error, "error should be nil")
  257. }
  258. func testThatExpiredCertificateRequestSucceedsWhenPinningIntermediateCAPublicKeyWithoutCertificateChainValidation() {
  259. // Given
  260. let publicKeys = [TestPublicKeys.IntermediateCA]
  261. let policies: [String: ServerTrustPolicy] = [
  262. host: .PinPublicKeys(publicKeys: publicKeys, validateCertificateChain: false, validateHost: true)
  263. ]
  264. let manager = Manager(
  265. configuration: configuration,
  266. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  267. )
  268. let expectation = expectationWithDescription("\(URL)")
  269. var error: NSError?
  270. // When
  271. manager.request(.GET, URL)
  272. .response { _, _, _, responseError in
  273. error = responseError
  274. expectation.fulfill()
  275. }
  276. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  277. // Then
  278. XCTAssertNil(error, "error should be nil")
  279. }
  280. func testThatExpiredCertificateRequestSucceedsWhenPinningRootCAPublicKeyWithoutCertificateChainValidation() {
  281. // Given
  282. let publicKeys = [TestPublicKeys.RootCA]
  283. let policies: [String: ServerTrustPolicy] = [
  284. host: .PinPublicKeys(publicKeys: publicKeys, validateCertificateChain: false, validateHost: true)
  285. ]
  286. let manager = Manager(
  287. configuration: configuration,
  288. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  289. )
  290. let expectation = expectationWithDescription("\(URL)")
  291. var error: NSError?
  292. // When
  293. manager.request(.GET, URL)
  294. .response { _, _, _, responseError in
  295. error = responseError
  296. expectation.fulfill()
  297. }
  298. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  299. // Then
  300. XCTAssertNil(error, "error should be nil")
  301. }
  302. // MARK: Server Trust Policy - Disabling Evaluation Tests
  303. func testThatExpiredCertificateRequestSucceedsWhenDisablingEvaluation() {
  304. // Given
  305. let policies = [host: ServerTrustPolicy.DisableEvaluation]
  306. let manager = Manager(
  307. configuration: configuration,
  308. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  309. )
  310. let expectation = expectationWithDescription("\(URL)")
  311. var error: NSError?
  312. // When
  313. manager.request(.GET, URL)
  314. .response { _, _, _, responseError in
  315. error = responseError
  316. expectation.fulfill()
  317. }
  318. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  319. // Then
  320. XCTAssertNil(error, "error should be nil")
  321. }
  322. // MARK: Server Trust Policy - Custom Evaluation Tests
  323. func testThatExpiredCertificateRequestSucceedsWhenCustomEvaluationReturnsTrue() {
  324. // Given
  325. let policies = [
  326. host: ServerTrustPolicy.CustomEvaluation { _, _ in
  327. // Implement a custom evaluation routine here...
  328. return true
  329. }
  330. ]
  331. let manager = Manager(
  332. configuration: configuration,
  333. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  334. )
  335. let expectation = expectationWithDescription("\(URL)")
  336. var error: NSError?
  337. // When
  338. manager.request(.GET, URL)
  339. .response { _, _, _, responseError in
  340. error = responseError
  341. expectation.fulfill()
  342. }
  343. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  344. // Then
  345. XCTAssertNil(error, "error should be nil")
  346. }
  347. func testThatExpiredCertificateRequestFailsWhenCustomEvaluationReturnsFalse() {
  348. // Given
  349. let policies = [
  350. host: ServerTrustPolicy.CustomEvaluation { _, _ in
  351. // Implement a custom evaluation routine here...
  352. return false
  353. }
  354. ]
  355. let manager = Manager(
  356. configuration: configuration,
  357. serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
  358. )
  359. let expectation = expectationWithDescription("\(URL)")
  360. var error: NSError?
  361. // When
  362. manager.request(.GET, URL)
  363. .response { _, _, _, responseError in
  364. error = responseError
  365. expectation.fulfill()
  366. }
  367. waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
  368. // Then
  369. XCTAssertNotNil(error, "error should not be nil")
  370. XCTAssertEqual(error?.code ?? -1, NSURLErrorCancelled, "error should be NSURLErrorCancelled")
  371. }
  372. }