ServerTrustEvaluatorTests.swift 60 KB

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