From f0cafaed95ccccdf47134a55284bcd1d9237a207 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Tue, 7 Mar 2023 10:09:10 +0000 Subject: [PATCH] Test that the right policy does the rejecting --- .../X509/Verifier/RFC5280/ExpiryPolicy.swift | 46 ++++++ .../X509/Verifier/RFC5280/RFC5280Policy.swift | 26 +-- Tests/X509Tests/RFC5280PolicyTests.swift | 150 +++++++++++++----- 3 files changed, 159 insertions(+), 63 deletions(-) create mode 100644 Sources/X509/Verifier/RFC5280/ExpiryPolicy.swift diff --git a/Sources/X509/Verifier/RFC5280/ExpiryPolicy.swift b/Sources/X509/Verifier/RFC5280/ExpiryPolicy.swift new file mode 100644 index 0000000..6e7c144 --- /dev/null +++ b/Sources/X509/Verifier/RFC5280/ExpiryPolicy.swift @@ -0,0 +1,46 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftCertificates open source project +// +// Copyright (c) 2023 Apple Inc. and the SwiftCertificates project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// +import Foundation + +/// A sub-policy of the ``RFC5280Policy`` that polices expiry. +@usableFromInline +struct ExpiryPolicy: VerifierPolicy { + @usableFromInline + let validationTime: Date + + @inlinable + init(validationTime: Date) { + self.validationTime = validationTime + } + + @inlinable + func chainMeetsPolicyRequirements(chain: UnverifiedCertificateChain) -> PolicyEvaluationResult { + // This is an easy check: confirm all the certs are valid. + for cert in chain { + if cert.notValidBefore > cert.notValidAfter { + return .failsToMeetPolicy(reason: "RFC5280Policy: Certificate \(cert) has invalid expiry, notValidAfter is earlier than notValidBefore") + } + + if self.validationTime < cert.notValidBefore { + return .failsToMeetPolicy(reason: "RFC5280Policy: Certificate \(cert) is not yet valid") + } + + if self.validationTime > cert.notValidAfter { + return .failsToMeetPolicy(reason: "RFC5280Policy: Certificate \(cert) has expired") + } + } + + return .meetsPolicy + } +} diff --git a/Sources/X509/Verifier/RFC5280/RFC5280Policy.swift b/Sources/X509/Verifier/RFC5280/RFC5280Policy.swift index cc8f246..51bad24 100644 --- a/Sources/X509/Verifier/RFC5280/RFC5280Policy.swift +++ b/Sources/X509/Verifier/RFC5280/RFC5280Policy.swift @@ -21,39 +21,19 @@ import Foundation /// 1. Expiry. Expired certificates are rejected. public struct RFC5280Policy: VerifierPolicy { @usableFromInline - let validationTime: Date + let expiryPolicy: ExpiryPolicy @inlinable public init(validationTime: Date) { - self.validationTime = validationTime + self.expiryPolicy = ExpiryPolicy(validationTime: validationTime) } @inlinable public func chainMeetsPolicyRequirements(chain: UnverifiedCertificateChain) -> PolicyEvaluationResult { - if case .failsToMeetPolicy(let reason) = self._validateExpiry(chain) { + if case .failsToMeetPolicy(let reason) = self.expiryPolicy.chainMeetsPolicyRequirements(chain: chain) { return .failsToMeetPolicy(reason: reason) } return .meetsPolicy } - - @inlinable - func _validateExpiry(_ chain: UnverifiedCertificateChain) -> PolicyEvaluationResult { - // This is an easy check: confirm all the certs are valid. - for cert in chain { - if cert.notValidBefore > cert.notValidAfter { - return .failsToMeetPolicy(reason: "RFC5280Policy: Certificate \(cert) has invalid expiry, notValidAfter is earlier than notValidBefore") - } - - if self.validationTime < cert.notValidBefore { - return .failsToMeetPolicy(reason: "RFC5280Policy: Certificate \(cert) is not yet valid") - } - - if self.validationTime > cert.notValidAfter { - return .failsToMeetPolicy(reason: "RFC5280Policy: Certificate \(cert) has expired") - } - } - - return .meetsPolicy - } } diff --git a/Tests/X509Tests/RFC5280PolicyTests.swift b/Tests/X509Tests/RFC5280PolicyTests.swift index 4fd0495..4782d48 100644 --- a/Tests/X509Tests/RFC5280PolicyTests.swift +++ b/Tests/X509Tests/RFC5280PolicyTests.swift @@ -15,10 +15,24 @@ import Foundation import XCTest import SwiftASN1 -import X509 +@testable import X509 import Crypto final class RFC5280PolicyTests: XCTestCase { + enum PolicyFactory { + case rfc5280 + case expiry + + func create(_ validationTime: Date) -> VerifierPolicy { + switch self { + case .rfc5280: + return RFC5280Policy(validationTime: validationTime) + case .expiry: + return ExpiryPolicy(validationTime: validationTime) + } + } + } + func testValidCertsAreAccepted() async throws { let roots = CertificateStore([TestPKI.unconstrainedCA]) let leaf = TestPKI.issueLeaf(issuer: .unconstrainedIntermediate) @@ -34,7 +48,7 @@ final class RFC5280PolicyTests: XCTestCase { XCTAssertEqual(chain, [leaf, TestPKI.unconstrainedIntermediate, TestPKI.unconstrainedCA]) } - func testExpiredLeafIsRejected() async throws { + private func _expiredLeafIsRejected(_ policyFactory: PolicyFactory) async throws { let roots = CertificateStore([TestPKI.unconstrainedCA]) let leaf = TestPKI.issueLeaf( notValidBefore: TestPKI.startDate + 1.0, @@ -43,7 +57,37 @@ final class RFC5280PolicyTests: XCTestCase { ) var verifier = Verifier( - rootCertificates: roots, policy: PolicySet(policies: [RFC5280Policy(validationTime: TestPKI.startDate + 3.0)]) + rootCertificates: roots, policy: PolicySet(policies: [policyFactory.create(TestPKI.startDate + 3.0)]) + ) + let result = await verifier.validate(leafCertificate: leaf, intermediates: CertificateStore([TestPKI.unconstrainedIntermediate])) + + guard case .couldNotValidate(let policyFailures) = result else { + XCTFail("Failed to validate: \(result)") + return + } + + XCTAssertEqual(policyFailures.count, 1) + } + + func testExpiredLeafIsRejected() async throws { + try await self._expiredLeafIsRejected(.rfc5280) + } + + func testExpiredLeafIsRejectedBasePolicy() async throws { + try await self._expiredLeafIsRejected(.expiry) + } + + func _expiredIntermediateIsRejected(_ policyFactory: PolicyFactory) async throws { + let roots = CertificateStore([TestPKI.unconstrainedCA]) + let leaf = TestPKI.issueLeaf( + notValidBefore: TestPKI.startDate, + notValidAfter: TestPKI.unconstrainedIntermediate.notValidAfter + 2.0, // Later than the intermediate. + issuer: .unconstrainedIntermediate + ) + + var verifier = Verifier( + rootCertificates: roots, + policy: PolicySet(policies: [policyFactory.create(TestPKI.unconstrainedIntermediate.notValidAfter + 1.0)]) ) let result = await verifier.validate(leafCertificate: leaf, intermediates: CertificateStore([TestPKI.unconstrainedIntermediate])) @@ -56,16 +100,24 @@ final class RFC5280PolicyTests: XCTestCase { } func testExpiredIntermediateIsRejected() async throws { + try await self._expiredIntermediateIsRejected(.rfc5280) + } + + func testExpiredIntermediateIsRejectedBasePolicy() async throws { + try await self._expiredIntermediateIsRejected(.expiry) + } + + func _expiredRootIsRejected(_ policyFactory: PolicyFactory) async throws { let roots = CertificateStore([TestPKI.unconstrainedCA]) let leaf = TestPKI.issueLeaf( notValidBefore: TestPKI.startDate, - notValidAfter: TestPKI.unconstrainedIntermediate.notValidAfter + 2.0, // Later than the intermediate. - issuer: .unconstrainedIntermediate + notValidAfter: TestPKI.unconstrainedCA.notValidAfter + 2.0, // Later than the root. + issuer: .unconstrainedRoot // Issue off the root directly to avoid the intermediate getting involved. ) var verifier = Verifier( rootCertificates: roots, - policy: PolicySet(policies: [RFC5280Policy(validationTime: TestPKI.unconstrainedIntermediate.notValidAfter + 1.0)]) + policy: PolicySet(policies: [policyFactory.create(TestPKI.unconstrainedCA.notValidAfter + 1.0)]) ) let result = await verifier.validate(leafCertificate: leaf, intermediates: CertificateStore([TestPKI.unconstrainedIntermediate])) @@ -78,16 +130,23 @@ final class RFC5280PolicyTests: XCTestCase { } func testExpiredRootIsRejected() async throws { + try await self._expiredRootIsRejected(.rfc5280) + } + + func testExpiredRootIsRejectedBasePolicy() async throws { + try await self._expiredRootIsRejected(.expiry) + } + + func _notYetValidLeafIsRejected(_ policyFactory: PolicyFactory) async throws { let roots = CertificateStore([TestPKI.unconstrainedCA]) let leaf = TestPKI.issueLeaf( - notValidBefore: TestPKI.startDate, - notValidAfter: TestPKI.unconstrainedCA.notValidAfter + 2.0, // Later than the root. - issuer: .unconstrainedRoot // Issue off the root directly to avoid the intermediate getting involved. + notValidBefore: TestPKI.startDate + 2.0, + notValidAfter: TestPKI.startDate + 3.0, // One second validity window + issuer: .unconstrainedIntermediate ) var verifier = Verifier( - rootCertificates: roots, - policy: PolicySet(policies: [RFC5280Policy(validationTime: TestPKI.unconstrainedCA.notValidAfter + 1.0)]) + rootCertificates: roots, policy: PolicySet(policies: [policyFactory.create(TestPKI.startDate + 1.0)]) ) let result = await verifier.validate(leafCertificate: leaf, intermediates: CertificateStore([TestPKI.unconstrainedIntermediate])) @@ -100,15 +159,24 @@ final class RFC5280PolicyTests: XCTestCase { } func testNotYetValidLeafIsRejected() async throws { + try await self._notYetValidLeafIsRejected(.rfc5280) + } + + func testNotYetValidLeafIsRejectedBasePolicy() async throws { + try await self._notYetValidLeafIsRejected(.expiry) + } + + func _notYetValidIntermediateIsRejected(_ policyFactory: PolicyFactory) async throws { let roots = CertificateStore([TestPKI.unconstrainedCA]) let leaf = TestPKI.issueLeaf( - notValidBefore: TestPKI.startDate + 2.0, - notValidAfter: TestPKI.startDate + 3.0, // One second validity window + notValidBefore: TestPKI.unconstrainedIntermediate.notValidBefore - 2.0, // Earlier than the intermediate + notValidAfter: TestPKI.unconstrainedIntermediate.notValidAfter, issuer: .unconstrainedIntermediate ) var verifier = Verifier( - rootCertificates: roots, policy: PolicySet(policies: [RFC5280Policy(validationTime: TestPKI.startDate + 1.0)]) + rootCertificates: roots, + policy: PolicySet(policies: [policyFactory.create(TestPKI.unconstrainedIntermediate.notValidBefore - 1.0)]) ) let result = await verifier.validate(leafCertificate: leaf, intermediates: CertificateStore([TestPKI.unconstrainedIntermediate])) @@ -121,16 +189,24 @@ final class RFC5280PolicyTests: XCTestCase { } func testNotYetValidIntermediateIsRejected() async throws { + try await self._notYetValidIntermediateIsRejected(.rfc5280) + } + + func testNotYetValidIntermediateIsRejectedBasePolicy() async throws { + try await self._notYetValidIntermediateIsRejected(.expiry) + } + + func _notYetValidRootIsRejected(_ policyFactory: PolicyFactory) async throws { let roots = CertificateStore([TestPKI.unconstrainedCA]) let leaf = TestPKI.issueLeaf( - notValidBefore: TestPKI.unconstrainedIntermediate.notValidBefore - 2.0, // Earlier than the intermediate - notValidAfter: TestPKI.unconstrainedIntermediate.notValidAfter, - issuer: .unconstrainedIntermediate + notValidBefore: TestPKI.unconstrainedCA.notValidBefore - 2.0, // Earlier than the root + notValidAfter: TestPKI.startDate, + issuer: .unconstrainedRoot // Issue off the root directly to avoid the intermediate getting involved. ) var verifier = Verifier( rootCertificates: roots, - policy: PolicySet(policies: [RFC5280Policy(validationTime: TestPKI.unconstrainedIntermediate.notValidBefore - 1.0)]) + policy: PolicySet(policies: [policyFactory.create(TestPKI.unconstrainedCA.notValidBefore - 1.0)]) ) let result = await verifier.validate(leafCertificate: leaf, intermediates: CertificateStore([TestPKI.unconstrainedIntermediate])) @@ -143,16 +219,23 @@ final class RFC5280PolicyTests: XCTestCase { } func testNotYetValidRootIsRejected() async throws { + try await self._notYetValidRootIsRejected(.rfc5280) + } + + func testNotYetValidRootIsRejectedBasePolicy() async throws { + try await self._notYetValidRootIsRejected(.expiry) + } + + func _malformedExpiryIsRejected(_ policyFactory: PolicyFactory) async throws { let roots = CertificateStore([TestPKI.unconstrainedCA]) let leaf = TestPKI.issueLeaf( - notValidBefore: TestPKI.unconstrainedCA.notValidBefore - 2.0, // Earlier than the root - notValidAfter: TestPKI.startDate, - issuer: .unconstrainedRoot // Issue off the root directly to avoid the intermediate getting involved. + notValidBefore: TestPKI.startDate + 3.0, + notValidAfter: TestPKI.startDate + 2.0, // invalid order + issuer: .unconstrainedIntermediate ) var verifier = Verifier( - rootCertificates: roots, - policy: PolicySet(policies: [RFC5280Policy(validationTime: TestPKI.unconstrainedCA.notValidBefore - 1.0)]) + rootCertificates: roots, policy: PolicySet(policies: [policyFactory.create(TestPKI.startDate + 2.5)]) ) let result = await verifier.validate(leafCertificate: leaf, intermediates: CertificateStore([TestPKI.unconstrainedIntermediate])) @@ -165,24 +248,11 @@ final class RFC5280PolicyTests: XCTestCase { } func testMalformedExpiryIsRejected() async throws { - let roots = CertificateStore([TestPKI.unconstrainedCA]) - let leaf = TestPKI.issueLeaf( - notValidBefore: TestPKI.startDate + 3.0, - notValidAfter: TestPKI.startDate + 2.0, // invalid order - issuer: .unconstrainedIntermediate - ) + try await self._malformedExpiryIsRejected(.rfc5280) + } - var verifier = Verifier( - rootCertificates: roots, policy: PolicySet(policies: [RFC5280Policy(validationTime: TestPKI.startDate + 2.5)]) - ) - let result = await verifier.validate(leafCertificate: leaf, intermediates: CertificateStore([TestPKI.unconstrainedIntermediate])) - - guard case .couldNotValidate(let policyFailures) = result else { - XCTFail("Failed to validate: \(result)") - return - } - - XCTAssertEqual(policyFailures.count, 1) + func testMalformedExpiryIsRejectedBasePolicy() async throws { + try await self._malformedExpiryIsRejected(.expiry) } }