TLSEvaluationTests.swift 17 KB

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