TLSEvaluationTests.swift 20 KB

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