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