ServerTrustEvaluatorTests.swift 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. //
  2. // ServerTrustEvaluatorTests.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. // Root Certificates
  31. static let rootCA = TestCertificates.certificate(filename: "alamofire-root-ca")
  32. // Intermediate Certificates
  33. static let intermediateCA1 = TestCertificates.certificate(filename: "alamofire-signing-ca1")
  34. static let intermediateCA2 = TestCertificates.certificate(filename: "alamofire-signing-ca2")
  35. // Leaf Certificates - Signed by CA1
  36. static let leafWildcard = TestCertificates.certificate(filename: "wildcard.alamofire.org")
  37. static let leafMultipleDNSNames = TestCertificates.certificate(filename: "multiple-dns-names")
  38. static let leafSignedByCA1 = TestCertificates.certificate(filename: "signed-by-ca1")
  39. static let leafDNSNameAndURI = TestCertificates.certificate(filename: "test.alamofire.org")
  40. // Leaf Certificates - Signed by CA2
  41. static let leafExpired = TestCertificates.certificate(filename: "expired")
  42. static let leafMissingDNSNameAndURI = TestCertificates.certificate(filename: "missing-dns-name-and-uri")
  43. static let leafSignedByCA2 = TestCertificates.certificate(filename: "signed-by-ca2")
  44. static let leafValidDNSName = TestCertificates.certificate(filename: "valid-dns-name")
  45. static let leafValidURI = TestCertificates.certificate(filename: "valid-uri")
  46. static func certificate(filename: String) -> SecCertificate {
  47. let filePath = Bundle.test.path(forResource: filename, ofType: "cer")!
  48. let data = try! Data(contentsOf: URL(fileURLWithPath: filePath))
  49. return SecCertificateCreateWithData(nil, data as CFData)!
  50. }
  51. }
  52. // MARK: -
  53. private enum TestTrusts {
  54. // Leaf Trusts - Signed by CA1
  55. case leafWildcard
  56. case leafMultipleDNSNames
  57. case leafSignedByCA1
  58. case leafDNSNameAndURI
  59. // Leaf Trusts - Signed by CA2
  60. case leafExpired
  61. case leafMissingDNSNameAndURI
  62. case leafSignedByCA2
  63. case leafValidDNSName
  64. case leafValidURI
  65. // Invalid Trusts
  66. case leafValidDNSNameMissingIntermediate
  67. case leafValidDNSNameWithIncorrectIntermediate
  68. var trust: SecTrust {
  69. switch self {
  70. case .leafWildcard:
  71. TestTrusts.trustWithCertificates([TestCertificates.leafWildcard,
  72. TestCertificates.intermediateCA1,
  73. TestCertificates.rootCA])
  74. case .leafMultipleDNSNames:
  75. TestTrusts.trustWithCertificates([TestCertificates.leafMultipleDNSNames,
  76. TestCertificates.intermediateCA1,
  77. TestCertificates.rootCA])
  78. case .leafSignedByCA1:
  79. TestTrusts.trustWithCertificates([TestCertificates.leafSignedByCA1,
  80. TestCertificates.intermediateCA1,
  81. TestCertificates.rootCA])
  82. case .leafDNSNameAndURI:
  83. TestTrusts.trustWithCertificates([TestCertificates.leafDNSNameAndURI,
  84. TestCertificates.intermediateCA1,
  85. TestCertificates.rootCA])
  86. case .leafExpired:
  87. TestTrusts.trustWithCertificates([TestCertificates.leafExpired,
  88. TestCertificates.intermediateCA2,
  89. TestCertificates.rootCA])
  90. case .leafMissingDNSNameAndURI:
  91. TestTrusts.trustWithCertificates([TestCertificates.leafMissingDNSNameAndURI,
  92. TestCertificates.intermediateCA2,
  93. TestCertificates.rootCA])
  94. case .leafSignedByCA2:
  95. TestTrusts.trustWithCertificates([TestCertificates.leafSignedByCA2,
  96. TestCertificates.intermediateCA2,
  97. TestCertificates.rootCA])
  98. case .leafValidDNSName:
  99. TestTrusts.trustWithCertificates([TestCertificates.leafValidDNSName,
  100. TestCertificates.intermediateCA2,
  101. TestCertificates.rootCA])
  102. case .leafValidURI:
  103. TestTrusts.trustWithCertificates([TestCertificates.leafValidURI,
  104. TestCertificates.intermediateCA2,
  105. TestCertificates.rootCA])
  106. case .leafValidDNSNameMissingIntermediate:
  107. TestTrusts.trustWithCertificates([TestCertificates.leafValidDNSName,
  108. TestCertificates.rootCA])
  109. case .leafValidDNSNameWithIncorrectIntermediate:
  110. TestTrusts.trustWithCertificates([TestCertificates.leafValidDNSName,
  111. TestCertificates.intermediateCA1,
  112. TestCertificates.rootCA])
  113. }
  114. }
  115. static func trustWithCertificates(_ certificates: [SecCertificate]) -> SecTrust {
  116. let policy = SecPolicyCreateBasicX509()
  117. var trust: SecTrust?
  118. SecTrustCreateWithCertificates(certificates as CFTypeRef, policy, &trust)
  119. return trust!
  120. }
  121. }
  122. // MARK: - Basic X509 and SSL Exploration Tests -
  123. class ServerTrustPolicyTestCase: BaseTestCase {
  124. func setRootCertificateAsLoneAnchorCertificateForTrust(_ trust: SecTrust) {
  125. SecTrustSetAnchorCertificates(trust, [TestCertificates.rootCA] as CFArray)
  126. SecTrustSetAnchorCertificatesOnly(trust, true)
  127. }
  128. }
  129. // MARK: - SecTrust Extension
  130. extension SecTrust {
  131. enum TrustError: Error { case invalid }
  132. /// Evaluates `self` and returns `true` if the evaluation succeeds with a value of `.unspecified` or `.proceed`.
  133. var isValid: Bool {
  134. if #available(iOS 12, macOS 10.14, tvOS 12, watchOS 5, visionOS 1, *) {
  135. Result { try af.evaluate() }.isSuccess
  136. } else {
  137. Result { try af.validate { _, _ in TrustError.invalid } }.isSuccess
  138. }
  139. }
  140. }
  141. // MARK: -
  142. class ServerTrustPolicyExplorationBasicX509PolicyValidationTestCase: ServerTrustPolicyTestCase {
  143. // func testThatAnchoredRootCertificatePassesBasicX509ValidationWithRootInTrust() {
  144. // // Given
  145. // let trust = TestTrusts.trustWithCertificates([TestCertificates.leafDNSNameAndURI,
  146. // TestCertificates.intermediateCA1,
  147. // TestCertificates.rootCA])
  148. //
  149. // setRootCertificateAsLoneAnchorCertificateForTrust(trust)
  150. //
  151. // // When
  152. // let policies = [SecPolicyCreateBasicX509()]
  153. // SecTrustSetPolicies(trust, policies as CFTypeRef)
  154. //
  155. // // Then
  156. // XCTAssertTrue(trust.isValid, "trust should be valid")
  157. // }
  158. //
  159. // func testThatAnchoredRootCertificatePassesBasicX509ValidationWithoutRootInTrust() {
  160. // // Given
  161. // let trust = TestTrusts.leafDNSNameAndURI.trust
  162. // setRootCertificateAsLoneAnchorCertificateForTrust(trust)
  163. //
  164. // // When
  165. // let policies = [SecPolicyCreateBasicX509()]
  166. // SecTrustSetPolicies(trust, policies as CFTypeRef)
  167. //
  168. // // Then
  169. // XCTAssertTrue(trust.isValid, "trust should be valid")
  170. // }
  171. //
  172. // func testThatCertificateMissingDNSNamePassesBasicX509Validation() {
  173. // // Given
  174. // let trust = TestTrusts.leafMissingDNSNameAndURI.trust
  175. // setRootCertificateAsLoneAnchorCertificateForTrust(trust)
  176. //
  177. // // When
  178. // let policies = [SecPolicyCreateBasicX509()]
  179. // SecTrustSetPolicies(trust, policies as CFTypeRef)
  180. //
  181. // // Then
  182. // XCTAssertTrue(trust.isValid, "trust should be valid")
  183. // }
  184. func testThatExpiredCertificateFailsBasicX509Validation() {
  185. // Given
  186. let trust = TestTrusts.leafExpired.trust
  187. setRootCertificateAsLoneAnchorCertificateForTrust(trust)
  188. // When
  189. let policies = [SecPolicyCreateBasicX509()]
  190. SecTrustSetPolicies(trust, policies as CFTypeRef)
  191. // Then
  192. XCTAssertFalse(trust.isValid, "trust should not be valid")
  193. }
  194. }
  195. // MARK: -
  196. class ServerTrustPolicyExplorationSSLPolicyValidationTestCase: ServerTrustPolicyTestCase {
  197. // func testThatAnchoredRootCertificatePassesSSLValidationWithRootInTrust() {
  198. // // Given
  199. // let trust = TestTrusts.trustWithCertificates([TestCertificates.leafDNSNameAndURI,
  200. // TestCertificates.intermediateCA1,
  201. // TestCertificates.rootCA])
  202. //
  203. // setRootCertificateAsLoneAnchorCertificateForTrust(trust)
  204. //
  205. // // When
  206. // let policies = [SecPolicyCreateSSL(true, "test.alamofire.org" as CFString)]
  207. // SecTrustSetPolicies(trust, policies as CFTypeRef)
  208. //
  209. // // Then
  210. // XCTAssertTrue(trust.isValid, "trust should be valid")
  211. // }
  212. //
  213. // func testThatAnchoredRootCertificatePassesSSLValidationWithoutRootInTrust() {
  214. // // Given
  215. // let trust = TestTrusts.leafDNSNameAndURI.trust
  216. // setRootCertificateAsLoneAnchorCertificateForTrust(trust)
  217. //
  218. // // When
  219. // let policies = [SecPolicyCreateSSL(true, "test.alamofire.org" as CFString)]
  220. // SecTrustSetPolicies(trust, policies as CFTypeRef)
  221. //
  222. // // Then
  223. // XCTAssertTrue(trust.isValid, "trust should be valid")
  224. // }
  225. func testThatCertificateMissingDNSNameFailsSSLValidation() {
  226. // Given
  227. let trust = TestTrusts.leafMissingDNSNameAndURI.trust
  228. setRootCertificateAsLoneAnchorCertificateForTrust(trust)
  229. // When
  230. let policies = [SecPolicyCreateSSL(true, "test.alamofire.org" as CFString)]
  231. SecTrustSetPolicies(trust, policies as CFTypeRef)
  232. // Then
  233. XCTAssertFalse(trust.isValid, "trust should not be valid")
  234. }
  235. // func testThatWildcardCertificatePassesSSLValidation() {
  236. // // Given
  237. // let trust = TestTrusts.leafWildcard.trust // *.alamofire.org
  238. // setRootCertificateAsLoneAnchorCertificateForTrust(trust)
  239. //
  240. // // When
  241. // let policies = [SecPolicyCreateSSL(true, "test.alamofire.org" as CFString)]
  242. // SecTrustSetPolicies(trust, policies as CFTypeRef)
  243. //
  244. // // Then
  245. // XCTAssertTrue(trust.isValid, "trust should be valid")
  246. // }
  247. //
  248. // func testThatDNSNameCertificatePassesSSLValidation() {
  249. // // Given
  250. // let trust = TestTrusts.leafValidDNSName.trust
  251. // setRootCertificateAsLoneAnchorCertificateForTrust(trust)
  252. //
  253. // // When
  254. // let policies = [SecPolicyCreateSSL(true, "test.alamofire.org" as CFString)]
  255. // SecTrustSetPolicies(trust, policies as CFTypeRef)
  256. //
  257. // // Then
  258. // XCTAssertTrue(trust.isValid, "trust should be valid")
  259. // }
  260. func testThatURICertificateFailsSSLValidation() {
  261. // Given
  262. let trust = TestTrusts.leafValidURI.trust
  263. setRootCertificateAsLoneAnchorCertificateForTrust(trust)
  264. // When
  265. let policies = [SecPolicyCreateSSL(true, "test.alamofire.org" as CFString)]
  266. SecTrustSetPolicies(trust, policies as CFTypeRef)
  267. // Then
  268. XCTAssertFalse(trust.isValid, "trust should not be valid")
  269. }
  270. // func testThatMultipleDNSNamesCertificatePassesSSLValidationForAllEntries() {
  271. // // Given
  272. // let trust = TestTrusts.leafMultipleDNSNames.trust
  273. // setRootCertificateAsLoneAnchorCertificateForTrust(trust)
  274. //
  275. // // When
  276. // let policies = [SecPolicyCreateSSL(true, "test.alamofire.org" as CFString),
  277. // SecPolicyCreateSSL(true, "blog.alamofire.org" as CFString),
  278. // SecPolicyCreateSSL(true, "www.alamofire.org" as CFString)]
  279. // SecTrustSetPolicies(trust, policies as CFTypeRef)
  280. //
  281. // // Then
  282. // XCTAssertTrue(trust.isValid, "trust should not be valid")
  283. // }
  284. //
  285. // func testThatPassingNilForHostParameterAllowsCertificateMissingDNSNameToPassSSLValidation() {
  286. // // Given
  287. // let trust = TestTrusts.leafMissingDNSNameAndURI.trust
  288. // setRootCertificateAsLoneAnchorCertificateForTrust(trust)
  289. //
  290. // // When
  291. // let policies = [SecPolicyCreateSSL(true, nil)]
  292. // SecTrustSetPolicies(trust, policies as CFTypeRef)
  293. //
  294. // // Then
  295. // XCTAssertTrue(trust.isValid, "trust should not be valid")
  296. // }
  297. func testThatExpiredCertificateFailsSSLValidation() {
  298. // Given
  299. let trust = TestTrusts.leafExpired.trust
  300. setRootCertificateAsLoneAnchorCertificateForTrust(trust)
  301. // When
  302. let policies = [SecPolicyCreateSSL(true, "test.alamofire.org" as CFString)]
  303. SecTrustSetPolicies(trust, policies as CFTypeRef)
  304. // Then
  305. XCTAssertFalse(trust.isValid, "trust should not be valid")
  306. }
  307. }
  308. // MARK: - Server Trust Policy Tests -
  309. class ServerTrustPolicyPerformDefaultEvaluationTestCase: ServerTrustPolicyTestCase {
  310. // MARK: Do NOT Validate Host
  311. // func testThatValidCertificateChainPassesEvaluationWithoutHostValidation() {
  312. // // Given
  313. // let host = "test.alamofire.org"
  314. // let serverTrust = TestTrusts.leafValidDNSName.trust
  315. // let serverTrustPolicy = DefaultTrustEvaluator(validateHost: false)
  316. //
  317. // // When
  318. // setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  319. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  320. //
  321. // // Then
  322. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  323. // }
  324. func testThatNonAnchoredRootCertificateChainFailsEvaluationWithoutHostValidation() {
  325. // Given
  326. let host = "test.alamofire.org"
  327. let serverTrust = TestTrusts.trustWithCertificates([TestCertificates.leafValidDNSName,
  328. TestCertificates.intermediateCA2])
  329. let serverTrustPolicy = DefaultTrustEvaluator(validateHost: false)
  330. // When
  331. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  332. // Then
  333. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  334. }
  335. // func testThatMissingDNSNameLeafCertificatePassesEvaluationWithoutHostValidation() {
  336. // // Given
  337. // let host = "test.alamofire.org"
  338. // let serverTrust = TestTrusts.leafMissingDNSNameAndURI.trust
  339. // let serverTrustPolicy = DefaultTrustEvaluator(validateHost: false)
  340. //
  341. // // When
  342. // setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  343. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  344. //
  345. // // Then
  346. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  347. // }
  348. func testThatExpiredCertificateChainFailsEvaluationWithoutHostValidation() {
  349. // Given
  350. let host = "test.alamofire.org"
  351. let serverTrust = TestTrusts.leafExpired.trust
  352. let serverTrustPolicy = DefaultTrustEvaluator(validateHost: false)
  353. // When
  354. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  355. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  356. // Then
  357. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  358. }
  359. func testThatMissingIntermediateCertificateInChainFailsEvaluationWithoutHostValidation() {
  360. // Given
  361. let host = "test.alamofire.org"
  362. let serverTrust = TestTrusts.leafValidDNSNameMissingIntermediate.trust
  363. let serverTrustPolicy = DefaultTrustEvaluator(validateHost: false)
  364. // When
  365. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  366. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  367. // Then
  368. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  369. }
  370. // MARK: Validate Host
  371. // func testThatValidCertificateChainPassesEvaluationWithHostValidation() {
  372. // // Given
  373. // let host = "test.alamofire.org"
  374. // let serverTrust = TestTrusts.leafValidDNSName.trust
  375. // let serverTrustPolicy = DefaultTrustEvaluator(validateHost: true)
  376. //
  377. // // When
  378. // setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  379. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  380. //
  381. // // Then
  382. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  383. // }
  384. func testThatNonAnchoredRootCertificateChainFailsEvaluationWithHostValidation() {
  385. // Given
  386. let host = "test.alamofire.org"
  387. let serverTrust = TestTrusts.trustWithCertificates([TestCertificates.leafValidDNSName,
  388. TestCertificates.intermediateCA2])
  389. let serverTrustPolicy = DefaultTrustEvaluator(validateHost: true)
  390. // When
  391. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  392. // Then
  393. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  394. }
  395. func testThatMissingDNSNameLeafCertificateFailsEvaluationWithHostValidation() {
  396. // Given
  397. let host = "test.alamofire.org"
  398. let serverTrust = TestTrusts.leafMissingDNSNameAndURI.trust
  399. let serverTrustPolicy = DefaultTrustEvaluator(validateHost: true)
  400. // When
  401. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  402. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  403. // Then
  404. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  405. }
  406. // func testThatWildcardedLeafCertificateChainPassesEvaluationWithHostValidation() {
  407. // // Given
  408. // let host = "test.alamofire.org"
  409. // let serverTrust = TestTrusts.leafWildcard.trust
  410. // let serverTrustPolicy = DefaultTrustEvaluator(validateHost: true)
  411. //
  412. // // When
  413. // setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  414. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  415. //
  416. // // Then
  417. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  418. // }
  419. func testThatExpiredCertificateChainFailsEvaluationWithHostValidation() {
  420. // Given
  421. let host = "test.alamofire.org"
  422. let serverTrust = TestTrusts.leafExpired.trust
  423. let serverTrustPolicy = DefaultTrustEvaluator(validateHost: true)
  424. // When
  425. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  426. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  427. // Then
  428. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  429. }
  430. func testThatMissingIntermediateCertificateInChainFailsEvaluationWithHostValidation() {
  431. // Given
  432. let host = "test.alamofire.org"
  433. let serverTrust = TestTrusts.leafValidDNSNameMissingIntermediate.trust
  434. let serverTrustPolicy = DefaultTrustEvaluator(validateHost: false)
  435. // When
  436. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  437. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  438. // Then
  439. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  440. XCTAssertEqual(result.failure?.asAFError?.isServerTrustEvaluationError, true)
  441. }
  442. }
  443. // MARK: -
  444. class ServerTrustPolicyPerformRevokedEvaluationTestCase: ServerTrustPolicyTestCase {
  445. // MARK: Do NOT Validate Host
  446. // func testThatValidCertificateChainPassesEvaluationWithoutHostValidation() {
  447. // // Given
  448. // let host = "test.alamofire.org"
  449. // let serverTrust = TestTrusts.leafValidDNSName.trust
  450. // let serverTrustPolicy = RevocationTrustEvaluator(validateHost: false)
  451. //
  452. // // When
  453. // setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  454. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  455. //
  456. // // Then
  457. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  458. // }
  459. func testThatNonAnchoredRootCertificateChainFailsEvaluationWithoutHostValidation() {
  460. // Given
  461. let host = "test.alamofire.org"
  462. let serverTrust = TestTrusts.trustWithCertificates([TestCertificates.leafValidDNSName,
  463. TestCertificates.intermediateCA2])
  464. let serverTrustPolicy = RevocationTrustEvaluator(validateHost: false)
  465. // When
  466. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  467. // Then
  468. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  469. }
  470. // func testThatMissingDNSNameLeafCertificatePassesEvaluationWithoutHostValidation() {
  471. // // Given
  472. // let host = "test.alamofire.org"
  473. // let serverTrust = TestTrusts.leafMissingDNSNameAndURI.trust
  474. // let serverTrustPolicy = RevocationTrustEvaluator(validateHost: false)
  475. //
  476. // // When
  477. // setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  478. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  479. //
  480. // // Then
  481. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  482. // }
  483. func testThatExpiredCertificateChainFailsEvaluationWithoutHostValidation() {
  484. // Given
  485. let host = "test.alamofire.org"
  486. let serverTrust = TestTrusts.leafExpired.trust
  487. let serverTrustPolicy = RevocationTrustEvaluator(validateHost: false)
  488. // When
  489. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  490. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  491. // Then
  492. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  493. }
  494. func testThatMissingIntermediateCertificateInChainFailsEvaluationWithoutHostValidation() {
  495. // Given
  496. let host = "test.alamofire.org"
  497. let serverTrust = TestTrusts.leafValidDNSNameMissingIntermediate.trust
  498. let serverTrustPolicy = RevocationTrustEvaluator(validateHost: false)
  499. // When
  500. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  501. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  502. // Then
  503. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  504. }
  505. // MARK: Validate Host
  506. // func testThatValidCertificateChainPassesEvaluationWithHostValidation() {
  507. // // Given
  508. // let host = "test.alamofire.org"
  509. // let serverTrust = TestTrusts.leafValidDNSName.trust
  510. // let serverTrustPolicy = RevocationTrustEvaluator()
  511. //
  512. // // When
  513. // setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  514. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  515. //
  516. // // Then
  517. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  518. // }
  519. func testThatNonAnchoredRootCertificateChainFailsEvaluationWithHostValidation() {
  520. // Given
  521. let host = "test.alamofire.org"
  522. let serverTrust = TestTrusts.trustWithCertificates([TestCertificates.leafValidDNSName,
  523. TestCertificates.intermediateCA2])
  524. let serverTrustPolicy = RevocationTrustEvaluator()
  525. // When
  526. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  527. // Then
  528. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  529. }
  530. func testThatMissingDNSNameLeafCertificateFailsEvaluationWithHostValidation() {
  531. // Given
  532. let host = "test.alamofire.org"
  533. let serverTrust = TestTrusts.leafMissingDNSNameAndURI.trust
  534. let serverTrustPolicy = RevocationTrustEvaluator()
  535. // When
  536. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  537. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  538. // Then
  539. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  540. }
  541. // func testThatWildcardedLeafCertificateChainPassesEvaluationWithHostValidation() {
  542. // // Given
  543. // let host = "test.alamofire.org"
  544. // let serverTrust = TestTrusts.leafWildcard.trust
  545. // let serverTrustPolicy = RevocationTrustEvaluator()
  546. //
  547. // // When
  548. // setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  549. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  550. //
  551. // // Then
  552. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  553. // }
  554. func testThatExpiredCertificateChainFailsEvaluationWithHostValidation() {
  555. // Given
  556. let host = "test.alamofire.org"
  557. let serverTrust = TestTrusts.leafExpired.trust
  558. let serverTrustPolicy = RevocationTrustEvaluator()
  559. // When
  560. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  561. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  562. // Then
  563. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  564. }
  565. func testThatMissingIntermediateCertificateInChainFailsEvaluationWithHostValidation() {
  566. // Given
  567. let host = "test.alamofire.org"
  568. let serverTrust = TestTrusts.leafValidDNSNameMissingIntermediate.trust
  569. let serverTrustPolicy = RevocationTrustEvaluator()
  570. // When
  571. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  572. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  573. // Then
  574. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  575. }
  576. }
  577. // MARK: -
  578. class ServerTrustPolicyPinCertificatesTestCase: ServerTrustPolicyTestCase {
  579. // MARK: Validate Certificate Chain Without Validating Host
  580. func testThatPinnedLeafCertificatePassesEvaluationWithoutHostValidation() {
  581. // Given
  582. let host = "test.alamofire.org"
  583. let serverTrust = TestTrusts.leafValidDNSName.trust
  584. let certificates = [TestCertificates.leafValidDNSName]
  585. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates,
  586. performDefaultValidation: false,
  587. validateHost: false)
  588. // When
  589. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  590. // Then
  591. XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  592. }
  593. func testThatPinnedIntermediateCertificatePassesEvaluationWithoutHostValidation() {
  594. // Given
  595. let host = "test.alamofire.org"
  596. let serverTrust = TestTrusts.leafValidDNSName.trust
  597. let certificates = [TestCertificates.intermediateCA2]
  598. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates,
  599. performDefaultValidation: false,
  600. validateHost: false)
  601. // When
  602. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  603. // Then
  604. XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  605. }
  606. func testThatPinnedRootCertificatePassesEvaluationWithoutHostValidation() {
  607. // Given
  608. let host = "test.alamofire.org"
  609. let serverTrust = TestTrusts.leafValidDNSName.trust
  610. let certificates = [TestCertificates.rootCA]
  611. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates,
  612. performDefaultValidation: false,
  613. validateHost: false)
  614. // When
  615. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  616. // Then
  617. XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  618. }
  619. func testThatPinningLeafCertificateNotInCertificateChainFailsEvaluationWithoutHostValidation() {
  620. // Given
  621. let host = "test.alamofire.org"
  622. let serverTrust = TestTrusts.leafValidDNSName.trust
  623. let certificates = [TestCertificates.leafSignedByCA2]
  624. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates,
  625. performDefaultValidation: true,
  626. validateHost: false)
  627. // When
  628. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  629. // Then
  630. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  631. }
  632. func testThatPinningIntermediateCertificateNotInCertificateChainFailsEvaluationWithoutHostValidation() {
  633. // Given
  634. let host = "test.alamofire.org"
  635. let serverTrust = TestTrusts.leafValidDNSName.trust
  636. let certificates = [TestCertificates.intermediateCA1]
  637. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates, validateHost: false)
  638. // When
  639. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  640. // Then
  641. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  642. }
  643. func testThatPinningExpiredLeafCertificateFailsEvaluationWithoutHostValidation() {
  644. // Given
  645. let host = "test.alamofire.org"
  646. let serverTrust = TestTrusts.leafExpired.trust
  647. let certificates = [TestCertificates.leafExpired]
  648. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates, validateHost: false)
  649. // When
  650. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  651. // Then
  652. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  653. }
  654. func testThatPinningIntermediateCertificateWithExpiredLeafCertificateFailsEvaluationWithoutHostValidation() {
  655. // Given
  656. let host = "test.alamofire.org"
  657. let serverTrust = TestTrusts.leafExpired.trust
  658. let certificates = [TestCertificates.intermediateCA2]
  659. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates, validateHost: false)
  660. // When
  661. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  662. // Then
  663. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  664. }
  665. // MARK: Validate Certificate Chain and Host
  666. // func testThatPinnedLeafCertificatePassesEvaluationWithSelfSignedSupportAndHostValidation() {
  667. // // Given
  668. // let host = "test.alamofire.org"
  669. // let serverTrust = TestTrusts.leafValidDNSName.trust
  670. // let certificates = [TestCertificates.leafValidDNSName]
  671. // let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates, acceptSelfSignedCertificates: true)
  672. //
  673. // // When
  674. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  675. //
  676. // // Then
  677. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  678. // }
  679. //
  680. // func testThatPinnedIntermediateCertificatePassesEvaluationWithSelfSignedSupportAndHostValidation() {
  681. // // Given
  682. // let host = "test.alamofire.org"
  683. // let serverTrust = TestTrusts.leafValidDNSName.trust
  684. // let certificates = [TestCertificates.intermediateCA2]
  685. // let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates, acceptSelfSignedCertificates: true)
  686. //
  687. // // When
  688. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  689. //
  690. // // Then
  691. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  692. // }
  693. //
  694. // func testThatPinnedRootCertificatePassesEvaluationWithSelfSignedSupportAndHostValidation() {
  695. // // Given
  696. // let host = "test.alamofire.org"
  697. // let serverTrust = TestTrusts.leafValidDNSName.trust
  698. // let certificates = [TestCertificates.rootCA]
  699. // let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates, acceptSelfSignedCertificates: true)
  700. //
  701. // // When
  702. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  703. //
  704. // // Then
  705. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  706. // }
  707. func testThatPinningLeafCertificateNotInCertificateChainFailsEvaluationWithHostValidation() {
  708. // Given
  709. let host = "test.alamofire.org"
  710. let serverTrust = TestTrusts.leafValidDNSName.trust
  711. let certificates = [TestCertificates.leafSignedByCA2]
  712. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates,
  713. performDefaultValidation: true,
  714. validateHost: true)
  715. // When
  716. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  717. // Then
  718. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  719. }
  720. func testThatPinningIntermediateCertificateNotInCertificateChainFailsEvaluationWithHostValidation() {
  721. // Given
  722. let host = "test.alamofire.org"
  723. let serverTrust = TestTrusts.leafValidDNSName.trust
  724. let certificates = [TestCertificates.intermediateCA1]
  725. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates)
  726. // When
  727. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  728. // Then
  729. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  730. }
  731. func testThatPinningExpiredLeafCertificateFailsEvaluationWithHostValidation() {
  732. // Given
  733. let host = "test.alamofire.org"
  734. let serverTrust = TestTrusts.leafExpired.trust
  735. let certificates = [TestCertificates.leafExpired]
  736. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates)
  737. // When
  738. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  739. // Then
  740. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  741. }
  742. func testThatPinningIntermediateCertificateWithExpiredLeafCertificateFailsEvaluationWithHostValidation() {
  743. // Given
  744. let host = "test.alamofire.org"
  745. let serverTrust = TestTrusts.leafExpired.trust
  746. let certificates = [TestCertificates.intermediateCA2]
  747. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates)
  748. // When
  749. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  750. // Then
  751. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  752. }
  753. // MARK: Do NOT Validate Certificate Chain or Host
  754. func testThatPinnedLeafCertificateWithoutCertificateChainValidationPassesEvaluation() {
  755. // Given
  756. let host = "test.alamofire.org"
  757. let serverTrust = TestTrusts.leafValidDNSName.trust
  758. let certificates = [TestCertificates.leafValidDNSName]
  759. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates,
  760. performDefaultValidation: false,
  761. validateHost: false)
  762. // When
  763. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  764. // Then
  765. XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  766. }
  767. func testThatPinnedIntermediateCertificateWithoutCertificateChainValidationPassesEvaluation() {
  768. // Given
  769. let host = "test.alamofire.org"
  770. let serverTrust = TestTrusts.leafValidDNSName.trust
  771. let certificates = [TestCertificates.intermediateCA2]
  772. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates,
  773. performDefaultValidation: false,
  774. validateHost: false)
  775. // When
  776. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  777. // Then
  778. XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  779. }
  780. func testThatPinnedRootCertificateWithoutCertificateChainValidationPassesEvaluation() {
  781. // Given
  782. let host = "test.alamofire.org"
  783. let serverTrust = TestTrusts.leafValidDNSName.trust
  784. let certificates = [TestCertificates.rootCA]
  785. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates,
  786. performDefaultValidation: false,
  787. validateHost: false)
  788. // When
  789. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  790. // Then
  791. XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  792. }
  793. func testThatPinningLeafCertificateNotInCertificateChainWithoutCertificateChainValidationFailsEvaluation() {
  794. // Given
  795. let host = "test.alamofire.org"
  796. let serverTrust = TestTrusts.leafValidDNSName.trust
  797. let certificates = [TestCertificates.leafSignedByCA2]
  798. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates,
  799. performDefaultValidation: false,
  800. validateHost: false)
  801. // When
  802. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  803. // Then
  804. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  805. }
  806. func testThatPinningIntermediateCertificateNotInCertificateChainWithoutCertificateChainValidationFailsEvaluation() {
  807. // Given
  808. let host = "test.alamofire.org"
  809. let serverTrust = TestTrusts.leafValidDNSName.trust
  810. let certificates = [TestCertificates.intermediateCA1]
  811. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates,
  812. performDefaultValidation: false,
  813. validateHost: false)
  814. // When
  815. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  816. // Then
  817. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  818. }
  819. func testThatPinningExpiredLeafCertificateWithoutCertificateChainValidationPassesEvaluation() {
  820. // Given
  821. let host = "test.alamofire.org"
  822. let serverTrust = TestTrusts.leafExpired.trust
  823. let certificates = [TestCertificates.leafExpired]
  824. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates,
  825. performDefaultValidation: false,
  826. validateHost: false)
  827. // When
  828. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  829. // Then
  830. XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  831. }
  832. func testThatPinningIntermediateCertificateWithExpiredLeafCertificateWithoutCertificateChainValidationPassesEvaluation() {
  833. // Given
  834. let host = "test.alamofire.org"
  835. let serverTrust = TestTrusts.leafExpired.trust
  836. let certificates = [TestCertificates.intermediateCA2]
  837. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates,
  838. performDefaultValidation: false,
  839. validateHost: false)
  840. // When
  841. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  842. // Then
  843. XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  844. }
  845. func testThatPinningRootCertificateWithExpiredLeafCertificateWithoutCertificateChainValidationPassesEvaluation() {
  846. // Given
  847. let host = "test.alamofire.org"
  848. let serverTrust = TestTrusts.leafExpired.trust
  849. let certificates = [TestCertificates.rootCA]
  850. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates,
  851. performDefaultValidation: false,
  852. validateHost: false)
  853. // When
  854. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  855. // Then
  856. XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  857. }
  858. func testThatPinningMultipleCertificatesWithoutCertificateChainValidationPassesEvaluation() {
  859. // Given
  860. let host = "test.alamofire.org"
  861. let serverTrust = TestTrusts.leafExpired.trust
  862. let certificates = [TestCertificates.leafMultipleDNSNames, // not in certificate chain
  863. TestCertificates.leafSignedByCA1, // not in certificate chain
  864. TestCertificates.leafExpired, // in certificate chain 👍🏼👍🏼
  865. TestCertificates.leafWildcard, // not in certificate chain
  866. TestCertificates.leafDNSNameAndURI // not in certificate chain
  867. ]
  868. let serverTrustPolicy = PinnedCertificatesTrustEvaluator(certificates: certificates,
  869. performDefaultValidation: false,
  870. validateHost: false)
  871. // When
  872. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  873. // Then
  874. XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  875. }
  876. }
  877. // MARK: -
  878. class ServerTrustPolicyPinPublicKeysTestCase: ServerTrustPolicyTestCase {
  879. // MARK: Validate Certificate Chain Without Validating Host
  880. // func testThatPinningLeafKeyPassesEvaluationWithoutHostValidation() {
  881. // // Given
  882. // let host = "test.alamofire.org"
  883. // let serverTrust = TestTrusts.leafValidDNSName.trust
  884. // let keys = [TestCertificates.leafValidDNSName].af.publicKeys
  885. // let serverTrustPolicy = PublicKeysTrustEvaluator(keys: keys, validateHost: false)
  886. //
  887. // // When
  888. // setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  889. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  890. //
  891. // // Then
  892. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  893. // }
  894. //
  895. // func testThatPinningIntermediateKeyPassesEvaluationWithoutHostValidation() {
  896. // // Given
  897. // let host = "test.alamofire.org"
  898. // let serverTrust = TestTrusts.leafValidDNSName.trust
  899. // let keys = [TestCertificates.intermediateCA2].af.publicKeys
  900. // let serverTrustPolicy = PublicKeysTrustEvaluator(keys: keys, validateHost: false)
  901. //
  902. // // When
  903. // setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  904. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  905. //
  906. // // Then
  907. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  908. // }
  909. //
  910. // func testThatPinningRootKeyPassesEvaluationWithoutHostValidation() {
  911. // // Given
  912. // let host = "test.alamofire.org"
  913. // let serverTrust = TestTrusts.leafValidDNSName.trust
  914. // let keys = [TestCertificates.rootCA].af.publicKeys
  915. // let serverTrustPolicy = PublicKeysTrustEvaluator(keys: keys, validateHost: false)
  916. //
  917. // // When
  918. // setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  919. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  920. //
  921. // // Then
  922. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  923. // }
  924. func testThatPinningKeyNotInCertificateChainFailsEvaluationWithoutHostValidation() {
  925. // Given
  926. let host = "test.alamofire.org"
  927. let serverTrust = TestTrusts.leafValidDNSName.trust
  928. let keys = [TestCertificates.leafSignedByCA2].af.publicKeys
  929. let serverTrustPolicy = PublicKeysTrustEvaluator(keys: keys, validateHost: false)
  930. // When
  931. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  932. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  933. // Then
  934. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  935. }
  936. // func testThatPinningBackupKeyPassesEvaluationWithoutHostValidation() {
  937. // // Given
  938. // let host = "test.alamofire.org"
  939. // let serverTrust = TestTrusts.leafValidDNSName.trust
  940. // let keys = [TestCertificates.leafSignedByCA1, TestCertificates.intermediateCA1, TestCertificates.leafValidDNSName].af.publicKeys
  941. // let serverTrustPolicy = PublicKeysTrustEvaluator(keys: keys, validateHost: false)
  942. //
  943. // // When
  944. // setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  945. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  946. //
  947. // // Then
  948. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  949. // }
  950. //
  951. // // MARK: Validate Certificate Chain and Host
  952. //
  953. // func testThatPinningLeafKeyPassesEvaluationWithHostValidation() {
  954. // // Given
  955. // let host = "test.alamofire.org"
  956. // let serverTrust = TestTrusts.leafValidDNSName.trust
  957. // let keys = [TestCertificates.leafValidDNSName].af.publicKeys
  958. // let serverTrustPolicy = PublicKeysTrustEvaluator(keys: keys)
  959. //
  960. // // When
  961. // setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  962. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  963. //
  964. // // Then
  965. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  966. // }
  967. //
  968. // func testThatPinningIntermediateKeyPassesEvaluationWithHostValidation() {
  969. // // Given
  970. // let host = "test.alamofire.org"
  971. // let serverTrust = TestTrusts.leafValidDNSName.trust
  972. // let keys = [TestCertificates.intermediateCA2].af.publicKeys
  973. // let serverTrustPolicy = PublicKeysTrustEvaluator(keys: keys)
  974. //
  975. // // When
  976. // setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  977. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  978. //
  979. // // Then
  980. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  981. // }
  982. //
  983. // func testThatPinningRootKeyPassesEvaluationWithHostValidation() {
  984. // // Given
  985. // let host = "test.alamofire.org"
  986. // let serverTrust = TestTrusts.leafValidDNSName.trust
  987. // let keys = [TestCertificates.rootCA].af.publicKeys
  988. // let serverTrustPolicy = PublicKeysTrustEvaluator(keys: keys)
  989. //
  990. // // When
  991. // setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  992. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  993. //
  994. // // Then
  995. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  996. // }
  997. func testThatPinningKeyNotInCertificateChainFailsEvaluationWithHostValidation() {
  998. // Given
  999. let host = "test.alamofire.org"
  1000. let serverTrust = TestTrusts.leafValidDNSName.trust
  1001. let keys = [TestCertificates.leafSignedByCA2].af.publicKeys
  1002. let serverTrustPolicy = PublicKeysTrustEvaluator(keys: keys)
  1003. // When
  1004. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  1005. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  1006. // Then
  1007. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  1008. }
  1009. // func testThatPinningBackupKeyPassesEvaluationWithHostValidation() {
  1010. // // Given
  1011. // let host = "test.alamofire.org"
  1012. // let serverTrust = TestTrusts.leafValidDNSName.trust
  1013. // let keys = [TestCertificates.leafSignedByCA1, TestCertificates.intermediateCA1, TestCertificates.leafValidDNSName].af.publicKeys
  1014. // let serverTrustPolicy = PublicKeysTrustEvaluator(keys: keys)
  1015. //
  1016. // // When
  1017. // setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  1018. // let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  1019. //
  1020. // // Then
  1021. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  1022. // }
  1023. // MARK: Do NOT perform default validation or validate host.
  1024. func testThatPinningLeafKeyWithoutCertificateChainValidationPassesEvaluationWithMissingIntermediateCertificate() {
  1025. // Given
  1026. let host = "test.alamofire.org"
  1027. let serverTrust = TestTrusts.leafValidDNSNameMissingIntermediate.trust
  1028. let keys = [TestCertificates.leafValidDNSName].af.publicKeys
  1029. let serverTrustPolicy = PublicKeysTrustEvaluator(keys: keys,
  1030. performDefaultValidation: false,
  1031. validateHost: false)
  1032. // When
  1033. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  1034. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  1035. // Then
  1036. XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  1037. }
  1038. func testThatPinningRootKeyWithoutCertificateChainValidationFailsEvaluationWithMissingIntermediateCertificate() {
  1039. // Given
  1040. let host = "test.alamofire.org"
  1041. let serverTrust = TestTrusts.leafValidDNSNameMissingIntermediate.trust
  1042. let keys = [TestCertificates.rootCA].af.publicKeys
  1043. let serverTrustPolicy = PublicKeysTrustEvaluator(keys: keys,
  1044. performDefaultValidation: false,
  1045. validateHost: false)
  1046. // When
  1047. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  1048. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  1049. // Then
  1050. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  1051. }
  1052. func testThatPinningLeafKeyWithoutCertificateChainValidationPassesEvaluationWithIncorrectIntermediateCertificate() {
  1053. // Given
  1054. let host = "test.alamofire.org"
  1055. let serverTrust = TestTrusts.leafValidDNSNameWithIncorrectIntermediate.trust
  1056. let keys = [TestCertificates.leafValidDNSName].af.publicKeys
  1057. let serverTrustPolicy = PublicKeysTrustEvaluator(keys: keys,
  1058. performDefaultValidation: false,
  1059. validateHost: false)
  1060. // When
  1061. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  1062. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  1063. // Then
  1064. XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  1065. }
  1066. func testThatPinningLeafKeyWithoutCertificateChainValidationPassesEvaluationWithExpiredLeafCertificate() {
  1067. // Given
  1068. let host = "test.alamofire.org"
  1069. let serverTrust = TestTrusts.leafExpired.trust
  1070. let keys = [TestCertificates.leafExpired].af.publicKeys
  1071. let serverTrustPolicy = PublicKeysTrustEvaluator(keys: keys,
  1072. performDefaultValidation: false,
  1073. validateHost: false)
  1074. // When
  1075. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  1076. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  1077. // Then
  1078. XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  1079. }
  1080. func testThatPinningIntermediateKeyWithoutCertificateChainValidationPassesEvaluationWithExpiredLeafCertificate() {
  1081. // Given
  1082. let host = "test.alamofire.org"
  1083. let serverTrust = TestTrusts.leafExpired.trust
  1084. let keys = [TestCertificates.intermediateCA2].af.publicKeys
  1085. let serverTrustPolicy = PublicKeysTrustEvaluator(keys: keys,
  1086. performDefaultValidation: false,
  1087. validateHost: false)
  1088. // When
  1089. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  1090. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  1091. // Then
  1092. XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  1093. }
  1094. func testThatPinningRootKeyWithoutCertificateChainValidationPassesEvaluationWithExpiredLeafCertificate() {
  1095. // Given
  1096. let host = "test.alamofire.org"
  1097. let serverTrust = TestTrusts.leafExpired.trust
  1098. let keys = [TestCertificates.rootCA].af.publicKeys
  1099. let serverTrustPolicy = PublicKeysTrustEvaluator(keys: keys,
  1100. performDefaultValidation: false,
  1101. validateHost: false)
  1102. // When
  1103. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  1104. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  1105. // Then
  1106. XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  1107. }
  1108. }
  1109. // MARK: -
  1110. class ServerTrustPolicyDisableEvaluationTestCase: ServerTrustPolicyTestCase {
  1111. func testThatCertificateChainMissingIntermediateCertificatePassesEvaluation() {
  1112. // Given
  1113. let host = "test.alamofire.org"
  1114. let serverTrust = TestTrusts.leafValidDNSNameMissingIntermediate.trust
  1115. let serverTrustPolicy = DisabledTrustEvaluator()
  1116. // When
  1117. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  1118. // Then
  1119. XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  1120. }
  1121. func testThatExpiredLeafCertificatePassesEvaluation() {
  1122. // Given
  1123. let host = "test.alamofire.org"
  1124. let serverTrust = TestTrusts.leafExpired.trust
  1125. let serverTrustPolicy = DisabledTrustEvaluator()
  1126. // When
  1127. let result = Result { try serverTrustPolicy.evaluate(serverTrust, forHost: host) }
  1128. // Then
  1129. XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  1130. }
  1131. }
  1132. // MARK: -
  1133. class ServerTrustPolicyCompositeTestCase: ServerTrustPolicyTestCase {
  1134. // func testThatValidCertificateChainPassesDefaultAndRevocationCompositeChecks() throws {
  1135. // // Given
  1136. // let host = "test.alamofire.org"
  1137. // let serverTrust = TestTrusts.leafValidDNSName.trust
  1138. // let defaultPolicy = DefaultTrustEvaluator(validateHost: false)
  1139. // let revocationPolicy = RevocationTrustEvaluator(validateHost: false)
  1140. // let compositePolicy = CompositeTrustEvaluator(evaluators: [defaultPolicy, revocationPolicy])
  1141. //
  1142. // // When
  1143. // setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  1144. // let result = Result { try compositePolicy.evaluate(serverTrust, forHost: host) }
  1145. //
  1146. // // Then
  1147. // XCTAssertTrue(result.isSuccess, "server trust should pass evaluation")
  1148. // }
  1149. func testThatNonAnchoredRootCertificateChainFailsEvaluationWithoutHostValidation() {
  1150. // Given
  1151. let host = "test.alamofire.org"
  1152. let serverTrust = TestTrusts.trustWithCertificates([TestCertificates.leafValidDNSName,
  1153. TestCertificates.intermediateCA2])
  1154. let defaultPolicy = DefaultTrustEvaluator(validateHost: false)
  1155. let revocationPolicy = RevocationTrustEvaluator(validateHost: false)
  1156. let compositePolicy = CompositeTrustEvaluator(evaluators: [defaultPolicy, revocationPolicy])
  1157. // When
  1158. let result = Result { try compositePolicy.evaluate(serverTrust, forHost: host) }
  1159. // Then
  1160. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  1161. }
  1162. func testThatExpiredLeafCertificateFailsDefaultAndRevocationComposite() {
  1163. // Given
  1164. let host = "test.alamofire.org"
  1165. let serverTrust = TestTrusts.leafExpired.trust
  1166. let defaultPolicy = DefaultTrustEvaluator(validateHost: false)
  1167. let revocationPolicy = RevocationTrustEvaluator(validateHost: false)
  1168. let compositePolicy = CompositeTrustEvaluator(evaluators: [defaultPolicy, revocationPolicy])
  1169. // When
  1170. setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
  1171. let result = Result { try compositePolicy.evaluate(serverTrust, forHost: host) }
  1172. // Then
  1173. XCTAssertFalse(result.isSuccess, "server trust should not pass evaluation")
  1174. }
  1175. }
  1176. // MARK: -
  1177. final class ServerTrustPolicyCertificatesInBundleTestCase: ServerTrustPolicyTestCase {
  1178. func testOnlyValidCertificatesAreDetected() {
  1179. // Given
  1180. // Files present in bundle in the form of type+encoding+extension [key|cert][DER|PEM].[cer|crt|der|key|pem]
  1181. // certDER.cer: DER-encoded well-formed certificate
  1182. // certDER.crt: DER-encoded well-formed certificate
  1183. // certDER.der: DER-encoded well-formed certificate
  1184. // certPEM.*: PEM-encoded well-formed certificates, expected to fail: Apple API only handles DER encoding
  1185. // devURandomGibberish.crt: Random data, should fail
  1186. // keyDER.der: DER-encoded key, not a certificate, should fail
  1187. // When
  1188. let certificates = Bundle.test.af.certificates
  1189. // Then
  1190. // Expectation: 19 well-formed certificates in the test bundle plus 4 invalid certificates.
  1191. #if os(macOS)
  1192. // For some reason, macOS is allowing all certificates to be considered valid. Need to file a
  1193. // rdar demonstrating this behavior.
  1194. if #available(macOS 10.12, *) {
  1195. XCTAssertEqual(certificates.count, 19, "Expected 19 well-formed certificates")
  1196. } else {
  1197. XCTAssertEqual(certificates.count, 23, "Expected 23 well-formed certificates")
  1198. }
  1199. #else
  1200. XCTAssertEqual(certificates.count, 19, "Expected 19 well-formed certificates")
  1201. #endif
  1202. }
  1203. }
  1204. final class StaticServerTrustAccessorTests: ServerTrustPolicyTestCase {
  1205. func consumeServerTrustEvaluator(_ evaluator: any ServerTrustEvaluating) {
  1206. _ = evaluator
  1207. }
  1208. func testThatRevocationEvaluatorCanBeCreatedStaticallyFromProtocol() {
  1209. // Given, When, Then
  1210. consumeServerTrustEvaluator(.revocationChecking())
  1211. }
  1212. func testThatPinnedCertificatesEvaluatorCanBeCreatedStaticallyFromProtocol() {
  1213. // Given, When, Then
  1214. consumeServerTrustEvaluator(.pinnedCertificates())
  1215. }
  1216. func testThatPublicKeysEvaluatorCanBeCreatedStaticallyFromProtocol() {
  1217. // Given, When, Then
  1218. consumeServerTrustEvaluator(.publicKeys())
  1219. }
  1220. }
  1221. #endif