TLSEvaluationTests.swift 21 KB

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