TLSEvaluationTests.swift 21 KB

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