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. let evaluators = [revokedHost: policy]
  192. let manager = Session(configuration: configuration,
  193. serverTrustManager: ServerTrustManager(evaluators: evaluators))
  194. let expectation = expectation(description: "\(revokedURLString)")
  195. var error: AFError?
  196. // When
  197. manager.request(revokedURLString)
  198. .response { resp in
  199. error = resp.error
  200. expectation.fulfill()
  201. }
  202. waitForExpectations(timeout: timeout)
  203. // Then
  204. XCTAssertNotNil(error, "error should not be nil")
  205. XCTAssertEqual(error?.isServerTrustEvaluationError, true)
  206. if case let .serverTrustEvaluationFailed(reason)? = error {
  207. if #available(iOS 12, macOS 10.14, tvOS 12, watchOS 5, *) {
  208. XCTAssertTrue(reason.isTrustEvaluationFailed, "should be .trustEvaluationFailed")
  209. } else {
  210. // Test seems flaky and can result in either of these failures, perhaps due to the OS actually checking?
  211. XCTAssertTrue(reason.isDefaultEvaluationFailed || reason.isRevocationCheckFailed,
  212. "should be .defaultEvaluationFailed or .revocationCheckFailed")
  213. }
  214. } else {
  215. XCTFail("error should be .serverTrustEvaluationFailed")
  216. }
  217. }
  218. #endif
  219. // MARK: Server Trust Policy - Certificate Pinning Tests
  220. @MainActor
  221. func testThatExpiredCertificateRequestFailsWhenPinningLeafCertificateWithCertificateChainValidation() {
  222. // Given
  223. let certificates = [TestCertificates.leaf]
  224. let evaluators = [expiredHost: PinnedCertificatesTrustEvaluator(certificates: certificates)]
  225. let manager = Session(configuration: configuration,
  226. serverTrustManager: ServerTrustManager(evaluators: evaluators))
  227. let expectation = expectation(description: "\(expiredURLString)")
  228. var error: AFError?
  229. // When
  230. manager.request(expiredURLString)
  231. .response { resp in
  232. error = resp.error
  233. expectation.fulfill()
  234. }
  235. waitForExpectations(timeout: timeout)
  236. // Then
  237. XCTAssertNotNil(error, "error should not be nil")
  238. XCTAssertEqual(error?.isServerTrustEvaluationError, true)
  239. if case let .serverTrustEvaluationFailed(reason)? = error {
  240. if #available(iOS 12, macOS 10.14, tvOS 12, watchOS 5, *) {
  241. XCTAssertTrue(reason.isTrustEvaluationFailed, "should be .trustEvaluationFailed")
  242. } else {
  243. XCTAssertTrue(reason.isDefaultEvaluationFailed, "should be .defaultEvaluationFailed")
  244. }
  245. } else {
  246. XCTFail("error should be .serverTrustEvaluationFailed")
  247. }
  248. }
  249. @MainActor
  250. func testThatExpiredCertificateRequestFailsWhenPinningAllCertificatesWithCertificateChainValidation() {
  251. // Given
  252. let certificates = [TestCertificates.leaf,
  253. TestCertificates.intermediateCA1,
  254. TestCertificates.intermediateCA2,
  255. TestCertificates.rootCA]
  256. let evaluators = [expiredHost: PinnedCertificatesTrustEvaluator(certificates: certificates)]
  257. let manager = Session(configuration: configuration,
  258. serverTrustManager: ServerTrustManager(evaluators: evaluators))
  259. let expectation = expectation(description: "\(expiredURLString)")
  260. var error: AFError?
  261. // When
  262. manager.request(expiredURLString)
  263. .response { resp in
  264. error = resp.error
  265. expectation.fulfill()
  266. }
  267. waitForExpectations(timeout: timeout)
  268. // Then
  269. XCTAssertNotNil(error, "error should not be nil")
  270. XCTAssertEqual(error?.isServerTrustEvaluationError, true)
  271. if case let .serverTrustEvaluationFailed(reason)? = error {
  272. if #available(iOS 12, macOS 10.14, tvOS 12, watchOS 5, *) {
  273. XCTAssertTrue(reason.isTrustEvaluationFailed, "should be .trustEvaluationFailed")
  274. } else {
  275. XCTAssertTrue(reason.isDefaultEvaluationFailed, "should be .defaultEvaluationFailed")
  276. }
  277. } else {
  278. XCTFail("error should be .serverTrustEvaluationFailed")
  279. }
  280. }
  281. @MainActor
  282. func testThatExpiredCertificateRequestSucceedsWhenPinningLeafCertificateWithoutCertificateChainOrHostValidation() {
  283. // Given
  284. let certificates = [TestCertificates.leaf]
  285. let evaluators = [expiredHost: PinnedCertificatesTrustEvaluator(certificates: certificates, performDefaultValidation: false, validateHost: false)]
  286. let manager = Session(configuration: configuration,
  287. serverTrustManager: ServerTrustManager(evaluators: evaluators))
  288. let expectation = expectation(description: "\(expiredURLString)")
  289. var error: (any Error)?
  290. // When
  291. manager.request(expiredURLString)
  292. .response { resp in
  293. error = resp.error
  294. expectation.fulfill()
  295. }
  296. waitForExpectations(timeout: timeout)
  297. // Then
  298. XCTAssertNil(error, "error should be nil")
  299. }
  300. @MainActor
  301. func testThatExpiredCertificateRequestSucceedsWhenPinningIntermediateCACertificateWithoutCertificateChainOrHostValidation() {
  302. // Given
  303. let certificates = [TestCertificates.intermediateCA2]
  304. let evaluators = [expiredHost: PinnedCertificatesTrustEvaluator(certificates: certificates, performDefaultValidation: false, validateHost: false)]
  305. let manager = Session(configuration: configuration,
  306. serverTrustManager: ServerTrustManager(evaluators: evaluators))
  307. let expectation = expectation(description: "\(expiredURLString)")
  308. var error: (any Error)?
  309. // When
  310. manager.request(expiredURLString)
  311. .response { resp in
  312. error = resp.error
  313. expectation.fulfill()
  314. }
  315. waitForExpectations(timeout: timeout)
  316. // Then
  317. XCTAssertNil(error, "error should be nil")
  318. }
  319. @MainActor
  320. func testThatExpiredCertificateRequestSucceedsWhenPinningRootCACertificateWithoutCertificateChainValidation() {
  321. // Given
  322. let certificates = [TestCertificates.rootCA]
  323. let evaluators = [expiredHost: PinnedCertificatesTrustEvaluator(certificates: certificates, performDefaultValidation: false)]
  324. let manager = Session(configuration: configuration,
  325. serverTrustManager: ServerTrustManager(evaluators: evaluators))
  326. let expectation = expectation(description: "\(expiredURLString)")
  327. var error: (any Error)?
  328. // When
  329. manager.request(expiredURLString)
  330. .response { resp in
  331. error = resp.error
  332. expectation.fulfill()
  333. }
  334. waitForExpectations(timeout: timeout)
  335. // Then
  336. if #available(iOS 10.1, macOS 10.12.0, tvOS 10.1, *) {
  337. XCTAssertNotNil(error, "error should not be nil")
  338. } else {
  339. XCTAssertNil(error, "error should be nil")
  340. }
  341. }
  342. // MARK: Server Trust Policy - Public Key Pinning Tests
  343. @MainActor
  344. func testThatExpiredCertificateRequestFailsWhenPinningLeafPublicKeyWithCertificateChainValidation() {
  345. // Given
  346. let keys = [TestCertificates.leaf].af.publicKeys
  347. let evaluators = [expiredHost: PublicKeysTrustEvaluator(keys: keys)]
  348. let manager = Session(configuration: configuration,
  349. serverTrustManager: ServerTrustManager(evaluators: evaluators))
  350. let expectation = expectation(description: "\(expiredURLString)")
  351. var error: AFError?
  352. // When
  353. manager.request(expiredURLString)
  354. .response { resp in
  355. error = resp.error
  356. expectation.fulfill()
  357. }
  358. waitForExpectations(timeout: timeout)
  359. // Then
  360. XCTAssertNotNil(error, "error should not be nil")
  361. XCTAssertEqual(error?.isServerTrustEvaluationError, true)
  362. if case let .serverTrustEvaluationFailed(reason)? = error {
  363. if #available(iOS 12, macOS 10.14, tvOS 12, watchOS 5, *) {
  364. XCTAssertTrue(reason.isTrustEvaluationFailed, "should be .trustEvaluationFailed")
  365. } else {
  366. XCTAssertTrue(reason.isDefaultEvaluationFailed, "should be .defaultEvaluationFailed")
  367. }
  368. } else {
  369. XCTFail("error should be .serverTrustEvaluationFailed")
  370. }
  371. }
  372. @MainActor
  373. func testThatExpiredCertificateRequestSucceedsWhenPinningLeafPublicKeyWithoutCertificateChainOrHostValidation() {
  374. // Given
  375. let keys = [TestCertificates.leaf].af.publicKeys
  376. let evaluators = [expiredHost: PublicKeysTrustEvaluator(keys: keys, performDefaultValidation: false, validateHost: false)]
  377. let manager = Session(configuration: configuration,
  378. serverTrustManager: ServerTrustManager(evaluators: evaluators))
  379. let expectation = expectation(description: "\(expiredURLString)")
  380. var error: (any Error)?
  381. // When
  382. manager.request(expiredURLString)
  383. .response { resp in
  384. error = resp.error
  385. expectation.fulfill()
  386. }
  387. waitForExpectations(timeout: timeout)
  388. // Then
  389. XCTAssertNil(error, "error should be nil")
  390. }
  391. @MainActor
  392. func testThatExpiredCertificateRequestSucceedsWhenPinningIntermediateCAPublicKeyWithoutCertificateChainOrHostValidation() {
  393. // Given
  394. let keys = [TestCertificates.intermediateCA2].af.publicKeys
  395. let evaluators = [expiredHost: PublicKeysTrustEvaluator(keys: keys, performDefaultValidation: false, validateHost: false)]
  396. let manager = Session(configuration: configuration,
  397. serverTrustManager: ServerTrustManager(evaluators: evaluators))
  398. let expectation = expectation(description: "\(expiredURLString)")
  399. var error: (any Error)?
  400. // When
  401. manager.request(expiredURLString)
  402. .response { resp in
  403. error = resp.error
  404. expectation.fulfill()
  405. }
  406. waitForExpectations(timeout: timeout)
  407. // Then
  408. XCTAssertNil(error, "error should be nil")
  409. }
  410. @MainActor
  411. func testThatExpiredCertificateRequestSucceedsWhenPinningRootCAPublicKeyWithoutCertificateChainValidation() {
  412. // Given
  413. let keys = [TestCertificates.rootCA].af.publicKeys
  414. let evaluators = [expiredHost: PublicKeysTrustEvaluator(keys: keys, performDefaultValidation: false, validateHost: false)]
  415. let manager = Session(configuration: configuration,
  416. serverTrustManager: ServerTrustManager(evaluators: evaluators))
  417. let expectation = expectation(description: "\(expiredURLString)")
  418. var error: (any Error)?
  419. // When
  420. manager.request(expiredURLString)
  421. .response { resp in
  422. error = resp.error
  423. expectation.fulfill()
  424. }
  425. waitForExpectations(timeout: timeout)
  426. // Then
  427. if #available(iOS 10.1, macOS 10.12.0, tvOS 10.1, *) {
  428. XCTAssertNotNil(error, "error should not be nil")
  429. } else {
  430. XCTAssertNil(error, "error should be nil")
  431. }
  432. }
  433. // MARK: Server Trust Policy - Disabling Evaluation Tests
  434. @MainActor
  435. func testThatExpiredCertificateRequestSucceedsWhenDisablingEvaluation() {
  436. // Given
  437. let evaluators = [expiredHost: DisabledTrustEvaluator()]
  438. let manager = Session(configuration: configuration,
  439. serverTrustManager: ServerTrustManager(evaluators: evaluators))
  440. let expectation = expectation(description: "\(expiredURLString)")
  441. var error: (any Error)?
  442. // When
  443. manager.request(expiredURLString)
  444. .response { resp in
  445. error = resp.error
  446. expectation.fulfill()
  447. }
  448. waitForExpectations(timeout: timeout)
  449. // Then
  450. XCTAssertNil(error, "error should be nil")
  451. }
  452. }
  453. #endif