TLSEvaluationTests.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. //
  2. // TLSEvaluationTests.swift
  3. //
  4. // Copyright (c) 2014-2018 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.certificate(filename: "expired.badssl.com-root-ca")
  29. static let intermediateCA1 = TestCertificates.certificate(filename: "expired.badssl.com-intermediate-ca-1")
  30. static let intermediateCA2 = TestCertificates.certificate(filename: "expired.badssl.com-intermediate-ca-2")
  31. static let leaf = TestCertificates.certificate(filename: "expired.badssl.com-leaf")
  32. static func certificate(filename: String) -> SecCertificate {
  33. class Locator {}
  34. let filePath = Bundle(for: Locator.self).path(forResource: filename, ofType: "cer")!
  35. let data = try! Data(contentsOf: URL(fileURLWithPath: filePath))
  36. let certificate = SecCertificateCreateWithData(nil, data as CFData)!
  37. return certificate
  38. }
  39. }
  40. // MARK: -
  41. class TLSEvaluationExpiredLeafCertificateTestCase: BaseTestCase {
  42. private let expiredURLString = "https://expired.badssl.com/"
  43. private let expiredHost = "expired.badssl.com"
  44. private let revokedURLString = "https://revoked.badssl.com"
  45. private let revokedHost = "revoked.badssl.com"
  46. private var configuration: URLSessionConfiguration!
  47. // MARK: Setup and Teardown
  48. override func setUp() {
  49. super.setUp()
  50. configuration = URLSessionConfiguration.ephemeral
  51. configuration.urlCache = nil
  52. configuration.urlCredentialStorage = nil
  53. }
  54. // MARK: Default Behavior Tests
  55. func testThatExpiredCertificateRequestFailsWithNoServerTrustPolicy() {
  56. // Given
  57. let expectation = self.expectation(description: "\(expiredURLString)")
  58. let manager = Session(configuration: configuration)
  59. var error: Error?
  60. // When
  61. manager.request(expiredURLString)
  62. .response { resp in
  63. error = resp.error
  64. expectation.fulfill()
  65. }
  66. waitForExpectations(timeout: timeout, handler: nil)
  67. // Then
  68. XCTAssertNotNil(error)
  69. if let error = error as? URLError {
  70. XCTAssertEqual(error.code, .serverCertificateUntrusted)
  71. } else if let error = error as NSError? {
  72. XCTAssertEqual(error.domain, kCFErrorDomainCFNetwork as String)
  73. XCTAssertEqual(error.code, Int(CFNetworkErrors.cfErrorHTTPSProxyConnectionFailure.rawValue))
  74. } else {
  75. XCTFail("error should be a URLError or NSError from CFNetwork")
  76. }
  77. }
  78. func disabled_testRevokedCertificateRequestBehaviorWithNoServerTrustPolicy() {
  79. // Disabled due to the instability of due revocation testing of default evaluation from all platforms. This
  80. // test is left for debugging purposes only. Should not be committed into the test suite while enabled.
  81. // Given
  82. let expectation = self.expectation(description: "\(revokedURLString)")
  83. let manager = Session(configuration: configuration)
  84. var error: Error?
  85. // When
  86. manager.request(revokedURLString)
  87. .response { resp in
  88. error = resp.error
  89. expectation.fulfill()
  90. }
  91. waitForExpectations(timeout: timeout, handler: nil)
  92. // Then
  93. if #available(iOS 10.1, macOS 10.12, tvOS 10.1, *) {
  94. // Apple appears to have started revocation tests as part of default evaluation in 10.1
  95. XCTAssertNotNil(error)
  96. } else {
  97. XCTAssertNil(error)
  98. }
  99. }
  100. // MARK: Server Trust Policy - Perform Default Tests
  101. func testThatExpiredCertificateRequestFailsWithDefaultServerTrustPolicy() {
  102. // Given
  103. let evaluators = [expiredHost: DefaultTrustEvaluator(validateHost: true)]
  104. let manager = Session(
  105. configuration: configuration,
  106. serverTrustManager: ServerTrustManager(evaluators: evaluators)
  107. )
  108. let expectation = self.expectation(description: "\(expiredURLString)")
  109. var error: Error?
  110. // When
  111. manager.request(expiredURLString)
  112. .response { resp in
  113. error = resp.error
  114. expectation.fulfill()
  115. }
  116. waitForExpectations(timeout: timeout, handler: nil)
  117. // Then
  118. XCTAssertNotNil(error, "error should not be nil")
  119. if let error = error?.asAFError {
  120. XCTAssertTrue(error.isServerTrustEvaluationError, "should be .certificatePinningFailed")
  121. } else {
  122. XCTFail("error should be an AFError")
  123. }
  124. }
  125. func disabled_testRevokedCertificateRequestBehaviorWithDefaultServerTrustPolicy() {
  126. // Disabled due to the instability of due revocation testing of default evaluation from all platforms. This
  127. // test is left for debugging purposes only. Should not be committed into the test suite while enabled.
  128. // Given
  129. let defaultPolicy = DefaultTrustEvaluator()
  130. let evaluators = [revokedHost: defaultPolicy]
  131. let manager = Session(
  132. configuration: configuration,
  133. serverTrustManager: ServerTrustManager(evaluators: evaluators)
  134. )
  135. let expectation = self.expectation(description: "\(revokedURLString)")
  136. var error: Error?
  137. // When
  138. manager.request(revokedURLString)
  139. .response { resp in
  140. error = resp.error
  141. expectation.fulfill()
  142. }
  143. waitForExpectations(timeout: timeout, handler: nil)
  144. // Then
  145. if #available(iOS 10.1, macOS 10.12, tvOS 10.1, *) {
  146. // Apple appears to have started revocation tests as part of default evaluation in 10.1
  147. XCTAssertNotNil(error)
  148. } else {
  149. XCTAssertNil(error)
  150. }
  151. }
  152. // MARK: Server Trust Policy - Perform Revoked Tests
  153. func testThatExpiredCertificateRequestFailsWithRevokedServerTrustPolicy() {
  154. // Given
  155. let policy = RevocationTrustEvaluator()
  156. let evaluators = [expiredHost: policy]
  157. let manager = Session(
  158. configuration: configuration,
  159. serverTrustManager: ServerTrustManager(evaluators: evaluators)
  160. )
  161. let expectation = self.expectation(description: "\(expiredURLString)")
  162. var error: Error?
  163. // When
  164. manager.request(expiredURLString)
  165. .response { resp in
  166. error = resp.error
  167. expectation.fulfill()
  168. }
  169. waitForExpectations(timeout: timeout, handler: nil)
  170. // Then
  171. XCTAssertNotNil(error, "error should not be nil")
  172. if let error = error?.asAFError {
  173. XCTAssertTrue(error.isServerTrustEvaluationError, "should be .certificatePinningFailed")
  174. } else {
  175. XCTFail("error should be an AFError")
  176. }
  177. }
  178. func testThatRevokedCertificateRequestFailsWithRevokedServerTrustPolicy() {
  179. // Given
  180. let policy = RevocationTrustEvaluator()
  181. let evaluators = [revokedHost: policy]
  182. let manager = Session(
  183. configuration: configuration,
  184. serverTrustManager: ServerTrustManager(evaluators: evaluators)
  185. )
  186. let expectation = self.expectation(description: "\(revokedURLString)")
  187. var error: Error?
  188. // When
  189. manager.request(revokedURLString)
  190. .response { resp in
  191. error = resp.error
  192. expectation.fulfill()
  193. }
  194. waitForExpectations(timeout: timeout, handler: nil)
  195. // Then
  196. XCTAssertNotNil(error, "error should not be nil")
  197. if let error = error?.asAFError {
  198. XCTAssertTrue(error.isServerTrustEvaluationError, "should be .certificatePinningFailed")
  199. } else {
  200. XCTFail("error should be an AFError")
  201. }
  202. }
  203. // MARK: Server Trust Policy - Certificate Pinning Tests
  204. func testThatExpiredCertificateRequestFailsWhenPinningLeafCertificateWithCertificateChainValidation() {
  205. // Given
  206. let certificates = [TestCertificates.leaf]
  207. let evaluators = [
  208. expiredHost: PinnedCertificatesTrustEvaluator(certificates: certificates)
  209. ]
  210. let manager = Session(
  211. configuration: configuration,
  212. serverTrustManager: ServerTrustManager(evaluators: evaluators)
  213. )
  214. let expectation = self.expectation(description: "\(expiredURLString)")
  215. var error: Error?
  216. // When
  217. manager.request(expiredURLString)
  218. .response { resp in
  219. error = resp.error
  220. expectation.fulfill()
  221. }
  222. waitForExpectations(timeout: timeout, handler: nil)
  223. // Then
  224. XCTAssertNotNil(error, "error should not be nil")
  225. if let error = error?.asAFError {
  226. XCTAssertTrue(error.isServerTrustEvaluationError, "should be .certificatePinningFailed")
  227. } else {
  228. XCTFail("error should be an AFError")
  229. }
  230. }
  231. func testThatExpiredCertificateRequestFailsWhenPinningAllCertificatesWithCertificateChainValidation() {
  232. // Given
  233. let certificates = [
  234. TestCertificates.leaf,
  235. TestCertificates.intermediateCA1,
  236. TestCertificates.intermediateCA2,
  237. TestCertificates.rootCA
  238. ]
  239. let evaluators = [
  240. expiredHost: PinnedCertificatesTrustEvaluator(certificates: certificates)
  241. ]
  242. let manager = Session(
  243. configuration: configuration,
  244. serverTrustManager: ServerTrustManager(evaluators: evaluators)
  245. )
  246. let expectation = self.expectation(description: "\(expiredURLString)")
  247. var error: Error?
  248. // When
  249. manager.request(expiredURLString)
  250. .response { resp in
  251. error = resp.error
  252. expectation.fulfill()
  253. }
  254. waitForExpectations(timeout: timeout, handler: nil)
  255. // Then
  256. XCTAssertNotNil(error, "error should not be nil")
  257. if let error = error?.asAFError {
  258. XCTAssertTrue(error.isServerTrustEvaluationError, "should be .certificatePinningFailed")
  259. } else {
  260. XCTFail("error should be an AFError")
  261. }
  262. }
  263. func testThatExpiredCertificateRequestSucceedsWhenPinningLeafCertificateWithoutCertificateChainOrHostValidation() {
  264. // Given
  265. let certificates = [TestCertificates.leaf]
  266. let evaluators = [
  267. expiredHost: PinnedCertificatesTrustEvaluator(certificates: certificates, performDefaultValidation: false, validateHost: false)
  268. ]
  269. let manager = Session(
  270. configuration: configuration,
  271. serverTrustManager: ServerTrustManager(evaluators: evaluators)
  272. )
  273. let expectation = self.expectation(description: "\(expiredURLString)")
  274. var error: Error?
  275. // When
  276. manager.request(expiredURLString)
  277. .response { resp in
  278. error = resp.error
  279. expectation.fulfill()
  280. }
  281. waitForExpectations(timeout: timeout, handler: nil)
  282. // Then
  283. XCTAssertNil(error, "error should be nil")
  284. }
  285. func testThatExpiredCertificateRequestSucceedsWhenPinningIntermediateCACertificateWithoutCertificateChainOrHostValidation() {
  286. // Given
  287. let certificates = [TestCertificates.intermediateCA2]
  288. let evaluators = [
  289. expiredHost: PinnedCertificatesTrustEvaluator(certificates: certificates, performDefaultValidation: false, validateHost: false)
  290. ]
  291. let manager = Session(
  292. configuration: configuration,
  293. serverTrustManager: ServerTrustManager(evaluators: evaluators)
  294. )
  295. let expectation = self.expectation(description: "\(expiredURLString)")
  296. var error: Error?
  297. // When
  298. manager.request(expiredURLString)
  299. .response { resp in
  300. error = resp.error
  301. expectation.fulfill()
  302. }
  303. waitForExpectations(timeout: timeout, handler: nil)
  304. // Then
  305. XCTAssertNil(error, "error should be nil")
  306. }
  307. func testThatExpiredCertificateRequestSucceedsWhenPinningRootCACertificateWithoutCertificateChainValidation() {
  308. // Given
  309. let certificates = [TestCertificates.rootCA]
  310. let evaluators = [
  311. expiredHost: PinnedCertificatesTrustEvaluator(certificates: certificates, performDefaultValidation: false)
  312. ]
  313. let manager = Session(
  314. configuration: configuration,
  315. serverTrustManager: ServerTrustManager(evaluators: evaluators)
  316. )
  317. let expectation = self.expectation(description: "\(expiredURLString)")
  318. var error: Error?
  319. // When
  320. manager.request(expiredURLString)
  321. .response { resp in
  322. error = resp.error
  323. expectation.fulfill()
  324. }
  325. waitForExpectations(timeout: timeout, handler: nil)
  326. // Then
  327. if #available(iOS 10.1, macOS 10.12.0, tvOS 10.1, *) {
  328. XCTAssertNotNil(error, "error should not be nil")
  329. } else {
  330. XCTAssertNil(error, "error should be nil")
  331. }
  332. }
  333. // MARK: Server Trust Policy - Public Key Pinning Tests
  334. func testThatExpiredCertificateRequestFailsWhenPinningLeafPublicKeyWithCertificateChainValidation() {
  335. // Given
  336. let keys = [TestCertificates.leaf].publicKeys
  337. let evaluators = [
  338. expiredHost: PublicKeysTrustEvaluator(keys: keys)
  339. ]
  340. let manager = Session(
  341. configuration: configuration,
  342. serverTrustManager: ServerTrustManager(evaluators: evaluators)
  343. )
  344. let expectation = self.expectation(description: "\(expiredURLString)")
  345. var error: Error?
  346. // When
  347. manager.request(expiredURLString)
  348. .response { resp in
  349. error = resp.error
  350. expectation.fulfill()
  351. }
  352. waitForExpectations(timeout: timeout, handler: nil)
  353. // Then
  354. XCTAssertNotNil(error, "error should not be nil")
  355. if let error = error?.asAFError {
  356. XCTAssertTrue(error.isServerTrustEvaluationError, "should be .certificatePinningFailed")
  357. } else {
  358. XCTFail("error should be an AFError")
  359. }
  360. }
  361. func testThatExpiredCertificateRequestSucceedsWhenPinningLeafPublicKeyWithoutCertificateChainOrHostValidation() {
  362. // Given
  363. let keys = [TestCertificates.leaf].publicKeys
  364. let evaluators = [
  365. expiredHost: PublicKeysTrustEvaluator(keys: keys, performDefaultValidation: false, validateHost: false)
  366. ]
  367. let manager = Session(
  368. configuration: configuration,
  369. serverTrustManager: ServerTrustManager(evaluators: evaluators)
  370. )
  371. let expectation = self.expectation(description: "\(expiredURLString)")
  372. var error: Error?
  373. // When
  374. manager.request(expiredURLString)
  375. .response { resp in
  376. error = resp.error
  377. expectation.fulfill()
  378. }
  379. waitForExpectations(timeout: timeout, handler: nil)
  380. // Then
  381. XCTAssertNil(error, "error should be nil")
  382. }
  383. func testThatExpiredCertificateRequestSucceedsWhenPinningIntermediateCAPublicKeyWithoutCertificateChainOrHostValidation() {
  384. // Given
  385. let keys = [TestCertificates.intermediateCA2].publicKeys
  386. let evaluators = [
  387. expiredHost: PublicKeysTrustEvaluator(keys: keys, performDefaultValidation: false, validateHost: false)
  388. ]
  389. let manager = Session(
  390. configuration: configuration,
  391. serverTrustManager: ServerTrustManager(evaluators: evaluators)
  392. )
  393. let expectation = self.expectation(description: "\(expiredURLString)")
  394. var error: Error?
  395. // When
  396. manager.request(expiredURLString)
  397. .response { resp in
  398. error = resp.error
  399. expectation.fulfill()
  400. }
  401. waitForExpectations(timeout: timeout, handler: nil)
  402. // Then
  403. XCTAssertNil(error, "error should be nil")
  404. }
  405. func testThatExpiredCertificateRequestSucceedsWhenPinningRootCAPublicKeyWithoutCertificateChainValidation() {
  406. // Given
  407. let keys = [TestCertificates.rootCA].publicKeys
  408. let evaluators = [
  409. expiredHost: PublicKeysTrustEvaluator(keys: keys, performDefaultValidation: false, validateHost: false)
  410. ]
  411. let manager = Session(
  412. configuration: configuration,
  413. serverTrustManager: ServerTrustManager(evaluators: evaluators)
  414. )
  415. let expectation = self.expectation(description: "\(expiredURLString)")
  416. var error: Error?
  417. // When
  418. manager.request(expiredURLString)
  419. .response { resp in
  420. error = resp.error
  421. expectation.fulfill()
  422. }
  423. waitForExpectations(timeout: timeout, handler: nil)
  424. // Then
  425. if #available(iOS 10.1, macOS 10.12.0, tvOS 10.1, *) {
  426. XCTAssertNotNil(error, "error should not be nil")
  427. } else {
  428. XCTAssertNil(error, "error should be nil")
  429. }
  430. }
  431. // MARK: Server Trust Policy - Disabling Evaluation Tests
  432. func testThatExpiredCertificateRequestSucceedsWhenDisablingEvaluation() {
  433. // Given
  434. let evaluators = [expiredHost: DisabledEvaluator()]
  435. let manager = Session(
  436. configuration: configuration,
  437. serverTrustManager: ServerTrustManager(evaluators: evaluators)
  438. )
  439. let expectation = self.expectation(description: "\(expiredURLString)")
  440. var error: Error?
  441. // When
  442. manager.request(expiredURLString)
  443. .response { resp in
  444. error = resp.error
  445. expectation.fulfill()
  446. }
  447. waitForExpectations(timeout: timeout, handler: nil)
  448. // Then
  449. XCTAssertNil(error, "error should be nil")
  450. }
  451. }