diff --git a/Package.swift b/Package.swift index 41816ec..21d8490 100644 --- a/Package.swift +++ b/Package.swift @@ -31,8 +31,8 @@ let package = Package( dependencies: [ .package(url: "https://github.com/apple/swift-crypto.git", from: "2.2.1"), // swift-asn1 repo is private, so we can't access it anonymously yet - // .package(url: "https://github.com/apple/swift-asn1.git", .upToNextMinor(from: "0.5.0")), - .package(url: "git@github.com:apple/swift-asn1.git", .upToNextMinor(from: "0.5.0")), + // .package(url: "https://github.com/apple/swift-asn1.git", .upToNextMinor(from: "0.6.0")), + .package(url: "git@github.com:apple/swift-asn1.git", .upToNextMinor(from: "0.6.0")), .package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"), ], targets: [ diff --git a/Sources/X509/CertificatePrivateKey.swift b/Sources/X509/CertificatePrivateKey.swift index 0e76c57..68b0f99 100644 --- a/Sources/X509/CertificatePrivateKey.swift +++ b/Sources/X509/CertificatePrivateKey.swift @@ -14,6 +14,7 @@ @preconcurrency import Crypto @preconcurrency import _CryptoExtras +import Foundation extension Certificate { /// A private key that can be used with a certificate. diff --git a/Sources/X509/CertificatePublicKey.swift b/Sources/X509/CertificatePublicKey.swift index a25dcb4..106c5dd 100644 --- a/Sources/X509/CertificatePublicKey.swift +++ b/Sources/X509/CertificatePublicKey.swift @@ -15,6 +15,7 @@ import SwiftASN1 @preconcurrency import Crypto @preconcurrency import _CryptoExtras +import Foundation extension Certificate { /// A public key that can be used with a certificate. diff --git a/Sources/X509/CryptographicMessageSyntax/CMSSignedData.swift b/Sources/X509/CryptographicMessageSyntax/CMSSignedData.swift index c1db119..b6a7778 100644 --- a/Sources/X509/CryptographicMessageSyntax/CMSSignedData.swift +++ b/Sources/X509/CryptographicMessageSyntax/CMSSignedData.swift @@ -42,10 +42,6 @@ import SwiftASN1 /// ``` /// - Note: At the moment we don't support `crls` (`RevocationInfoChoices`) struct CMSSignedData: DERImplicitlyTaggable, Hashable { - private enum Error: Swift.Error { - case multipleDigestAlgorithmsAreNotSupportedYet - case multipleSignerInfosAreNotSupportedYet - } static var defaultIdentifier: ASN1Identifier { .sequence } @@ -73,27 +69,17 @@ struct CMSSignedData: DERImplicitlyTaggable, Hashable { init(derEncoded: ASN1Node, withIdentifier identifier: ASN1Identifier) throws { self = try DER.sequence(derEncoded, identifier: identifier) { nodes in let version = try CMSVersion(rawValue: Int.init(derEncoded: &nodes)) - let digestAlgorithms = try DER.sequence(of: AlgorithmIdentifier.self, identifier: .set, nodes: &nodes) - // TODO: support multiple digest algorithms. For this we need to validate that the binary representation of each element is lexicographically sorted. - guard digestAlgorithms.count <= 1 else { - throw Error.multipleDigestAlgorithmsAreNotSupportedYet - } + let digestAlgorithms = try DER.set(of: AlgorithmIdentifier.self, identifier: .set, nodes: &nodes) let encapContentInfo = try CMSEncapsulatedContentInfo(derEncoded: &nodes) let certificates = try DER.optionalImplicitlyTagged(&nodes, tagNumber: 0, tagClass: .contextSpecific) { node in - // TODO: this is actually a SET OF so we need to verify that the binary representation of each element is lexicographically sorted. - try DER.sequence(of: Certificate.self, identifier: .init(tagWithNumber: 0, tagClass: .contextSpecific), rootNode: node) + try DER.set(of: Certificate.self, identifier: .init(tagWithNumber: 0, tagClass: .contextSpecific), rootNode: node) } // we need to skip this node even though we don't support it _ = DER.optionalImplicitlyTagged(&nodes, tagNumber: 1, tagClass: .contextSpecific) { _ in } - let signerInfos = try DER.sequence(of: CMSSignerInfo.self, identifier: .set, nodes: &nodes) - - // TODO: support multiple signer infos. For this we need to validate that the binary representation of each element is lexicographically sorted. - guard signerInfos.count <= 1 else { - throw Error.multipleSignerInfosAreNotSupportedYet - } + let signerInfos = try DER.set(of: CMSSignerInfo.self, identifier: .set, nodes: &nodes) return .init( version: version, @@ -108,21 +94,12 @@ struct CMSSignedData: DERImplicitlyTaggable, Hashable { func serialize(into coder: inout DER.Serializer, withIdentifier identifier: ASN1Identifier) throws { try coder.appendConstructedNode(identifier: identifier) { coder in try coder.serialize(version.rawValue) - guard self.digestAlgorithms.count <= 1 else { - throw Error.multipleDigestAlgorithmsAreNotSupportedYet - } - // TODO: this is actually a SET OF. We need to sort the binary representation of each element lexicographically before encoding. - try coder.serializeSequenceOf(self.digestAlgorithms, identifier: .set) + try coder.serializeSetOf(self.digestAlgorithms) try coder.serialize(self.encapContentInfo) if let certificates { - // TODO: this is actually a SET OF. We need to sort the binary representation of each element lexicographically before encoding. - try coder.serializeSequenceOf(certificates, identifier: .init(tagWithNumber: 0, tagClass: .contextSpecific)) + try coder.serializeSetOf(certificates, identifier: .init(tagWithNumber: 0, tagClass: .contextSpecific)) } - guard self.signerInfos.count <= 1 else { - throw Error.multipleSignerInfosAreNotSupportedYet - } - // TODO: this is actually a SET OF. We need to sort the binary representation of each element lexicographically before encoding. - try coder.serializeSequenceOf(self.signerInfos, identifier: .set) + try coder.serializeSetOf(self.signerInfos, identifier: .set) } } } diff --git a/Sources/X509/DistinguishedNameBuilder/CommonName.swift b/Sources/X509/DistinguishedNameBuilder/CommonName.swift index af6e49c..37c4c10 100644 --- a/Sources/X509/DistinguishedNameBuilder/CommonName.swift +++ b/Sources/X509/DistinguishedNameBuilder/CommonName.swift @@ -12,6 +12,8 @@ // //===----------------------------------------------------------------------===// +import SwiftASN1 + /// Set the Common Name (CN) of a ``DistinguishedName``. /// /// This type is used in ``DistinguishedNameBuilder`` contexts. diff --git a/Sources/X509/DistinguishedNameBuilder/CountryName.swift b/Sources/X509/DistinguishedNameBuilder/CountryName.swift index 55fa4d7..77b3b60 100644 --- a/Sources/X509/DistinguishedNameBuilder/CountryName.swift +++ b/Sources/X509/DistinguishedNameBuilder/CountryName.swift @@ -12,6 +12,8 @@ // //===----------------------------------------------------------------------===// +import SwiftASN1 + /// Set the Country Name (C) of a ``DistinguishedName``. /// /// This type is used in ``DistinguishedNameBuilder`` contexts. diff --git a/Sources/X509/DistinguishedNameBuilder/LocalityName.swift b/Sources/X509/DistinguishedNameBuilder/LocalityName.swift index faa4224..dbdec4d 100644 --- a/Sources/X509/DistinguishedNameBuilder/LocalityName.swift +++ b/Sources/X509/DistinguishedNameBuilder/LocalityName.swift @@ -12,6 +12,8 @@ // //===----------------------------------------------------------------------===// +import SwiftASN1 + /// Set the Locality Name (L) of a ``DistinguishedName``. /// /// This type is used in ``DistinguishedNameBuilder`` contexts. diff --git a/Sources/X509/DistinguishedNameBuilder/OrganizationName.swift b/Sources/X509/DistinguishedNameBuilder/OrganizationName.swift index 4cfad13..83f225e 100644 --- a/Sources/X509/DistinguishedNameBuilder/OrganizationName.swift +++ b/Sources/X509/DistinguishedNameBuilder/OrganizationName.swift @@ -12,6 +12,8 @@ // //===----------------------------------------------------------------------===// +import SwiftASN1 + /// Set the Organization Name (O) of a ``DistinguishedName``. /// /// This type is used in ``DistinguishedNameBuilder`` contexts. diff --git a/Sources/X509/DistinguishedNameBuilder/OrganizationalUnitName.swift b/Sources/X509/DistinguishedNameBuilder/OrganizationalUnitName.swift index 133176c..59fcb7a 100644 --- a/Sources/X509/DistinguishedNameBuilder/OrganizationalUnitName.swift +++ b/Sources/X509/DistinguishedNameBuilder/OrganizationalUnitName.swift @@ -12,6 +12,8 @@ // //===----------------------------------------------------------------------===// +import SwiftASN1 + /// Set the Organizational Unit Name (OU) of a ``DistinguishedName``. /// /// This type is used in ``DistinguishedNameBuilder`` contexts. diff --git a/Sources/X509/DistinguishedNameBuilder/StateOrProvinceName.swift b/Sources/X509/DistinguishedNameBuilder/StateOrProvinceName.swift index 345ced4..4ecefa5 100644 --- a/Sources/X509/DistinguishedNameBuilder/StateOrProvinceName.swift +++ b/Sources/X509/DistinguishedNameBuilder/StateOrProvinceName.swift @@ -12,6 +12,8 @@ // //===----------------------------------------------------------------------===// +import SwiftASN1 + /// Set the State or Province Name (ST) of a ``DistinguishedName``. /// /// This type is used in ``DistinguishedNameBuilder`` contexts. diff --git a/Sources/X509/DistinguishedNameBuilder/StreetAddress.swift b/Sources/X509/DistinguishedNameBuilder/StreetAddress.swift index 7bc9271..d0fb0a7 100644 --- a/Sources/X509/DistinguishedNameBuilder/StreetAddress.swift +++ b/Sources/X509/DistinguishedNameBuilder/StreetAddress.swift @@ -12,6 +12,8 @@ // //===----------------------------------------------------------------------===// +import SwiftASN1 + /// Set the Street Address (STREET) of a ``DistinguishedName``. /// /// This type is used in ``DistinguishedNameBuilder`` contexts. diff --git a/Sources/X509/OCSP/OCSPPolicy.swift b/Sources/X509/OCSP/OCSPPolicy.swift index 15fae42..379cd9b 100644 --- a/Sources/X509/OCSP/OCSPPolicy.swift +++ b/Sources/X509/OCSP/OCSPPolicy.swift @@ -14,9 +14,13 @@ import SwiftASN1 import Crypto -import protocol Foundation.DataProtocol -import struct Foundation.Date -import typealias Foundation.TimeInterval +#if canImport(Darwin) +import Foundation +#else +// swift-corelibs-foundation hasn't marked anything Sendable yet https://github.com/apple/swift-corelibs-foundation/issues/4687 +@preconcurrency import Foundation +#endif + public protocol OCSPRequester: Sendable { /// Called with an OCSP Request. @@ -77,7 +81,7 @@ enum OCSPRequestHashAlgorithm { } } -public struct OCSPVerifierPolicy: VerifierPolicy { +public struct OCSPVerifierPolicy: VerifierPolicy, Sendable { private var requester: Requester private var requestHashAlgorithm: OCSPRequestHashAlgorithm @@ -303,7 +307,7 @@ extension OCSPSingleResponse { /// - maxDuration: max execution duration in seconds of `operation` /// - operation: the task to start and cancel after `maxDuration` seconds /// - Returns: the result of `operation` -private func withTimeout( +private func withTimeout( _ maxDuration: TimeInterval, operation: @escaping @Sendable () async -> Result ) async -> Result { diff --git a/Sources/X509/RelativeDistinguishedName.swift b/Sources/X509/RelativeDistinguishedName.swift index b770fe0..24305f4 100644 --- a/Sources/X509/RelativeDistinguishedName.swift +++ b/Sources/X509/RelativeDistinguishedName.swift @@ -116,25 +116,12 @@ extension RelativeDistinguishedName: DERImplicitlyTaggable { @inlinable public init(derEncoded rootNode: ASN1Node, withIdentifier identifier: ASN1Identifier) throws { - self = try DER.set(rootNode, identifier: identifier) { nodes in - // TODO: We need to validate that these are lexicographically sorted. Perhaps ASN1.set should do it? - var elements = Array() - - while let node = nodes.next() { - try elements.append(.init(derEncoded: node)) - } - - return try RelativeDistinguishedName(elements) - } + try self.init(DER.set(identifier: identifier, rootNode: rootNode)) } @inlinable public func serialize(into coder: inout DER.Serializer, withIdentifier identifier: ASN1Identifier) throws { - try coder.appendConstructedNode(identifier: identifier) { rootCoder in - for element in self.attributes { - try element.serialize(into: &rootCoder) - } - } + try coder.serializeSetOf(self.attributes, identifier: identifier) } @inlinable diff --git a/Sources/X509/Signature.swift b/Sources/X509/Signature.swift index 8c658e9..76c38f1 100644 --- a/Sources/X509/Signature.swift +++ b/Sources/X509/Signature.swift @@ -15,6 +15,7 @@ import SwiftASN1 @preconcurrency import Crypto @preconcurrency import _CryptoExtras +import Foundation extension Certificate { /// An abstract representation of the cryptographic signature on a certificate. diff --git a/Sources/X509/Verifier/VerifierPolicy.swift b/Sources/X509/Verifier/VerifierPolicy.swift index 1b57e6c..4ba5a2a 100644 --- a/Sources/X509/Verifier/VerifierPolicy.swift +++ b/Sources/X509/Verifier/VerifierPolicy.swift @@ -16,7 +16,7 @@ public protocol VerifierPolicy { mutating func chainMeetsPolicyRequirements(chain: UnverifiedCertificateChain) async -> PolicyEvaluationResult } -public enum PolicyEvaluationResult { +public enum PolicyEvaluationResult: Sendable { case meetsPolicy case failsToMeetPolicy(reason: String) } diff --git a/Tests/X509Tests/DistinguishedNameTests.swift b/Tests/X509Tests/DistinguishedNameTests.swift index 6f1afcb..9361afe 100644 --- a/Tests/X509Tests/DistinguishedNameTests.swift +++ b/Tests/X509Tests/DistinguishedNameTests.swift @@ -159,7 +159,7 @@ final class DistinguishedNameTests: XCTestCase { StateOrProvinceName("DigiLand") } - for name in ["foo", "bar", "baz"].filter { $0 == "baz" } { + for name in ["foo", "bar", "baz"].filter({ $0 == "baz" }) { CommonName(name) } } diff --git a/Tests/X509Tests/OCSPPolicyVerifierTests.swift b/Tests/X509Tests/OCSPPolicyVerifierTests.swift index 7a063ed..0347ae6 100644 --- a/Tests/X509Tests/OCSPPolicyVerifierTests.swift +++ b/Tests/X509Tests/OCSPPolicyVerifierTests.swift @@ -16,7 +16,11 @@ import XCTest import Crypto import SwiftASN1 @testable import X509 +#if canImport(Darwin) import Foundation +#else +@preconcurrency import Foundation +#endif actor TestRequester: OCSPRequester { private let queryClosure: @Sendable (OCSPRequest, String) async throws -> OCSPResponse @@ -187,6 +191,7 @@ final class OCSPVerifierPolicyTests: XCTestCase { } func testSingleCertWithOCSP() async { + let now = self.now await self.assertChainMeetsPolicy( chain: Self.chainWithSingleCertWithOCSP, requester: .noThrow { request, uri -> OCSPResponse in @@ -198,14 +203,15 @@ final class OCSPVerifierPolicyTests: XCTestCase { return .successful(.signed(responses: [OCSPSingleResponse( certID: singleRequest.certID, certStatus: .good, - thisUpdate: try .init(self.now - .days(1)), - nextUpdate: try .init(self.now + .days(1)) + thisUpdate: try .init(now - .days(1)), + nextUpdate: try .init(now + .days(1)) )], responseExtensions: { nonce })) } ) } func testWrongNonce() async { + let now = self.now await self.assertChainFailsToMeetPolicy( chain: Self.chainWithSingleCertWithOCSP, requester: .noThrow { request, uri -> OCSPResponse in @@ -217,14 +223,15 @@ final class OCSPVerifierPolicyTests: XCTestCase { return .successful(.signed(responses: [OCSPSingleResponse( certID: singleRequest.certID, certStatus: .good, - thisUpdate: try .init(self.now - .days(1)), - nextUpdate: try .init(self.now + .days(1)) + thisUpdate: try .init(now - .days(1)), + nextUpdate: try .init(now + .days(1)) )], responseExtensions: { OCSPNonce() })) } ) } func testRevokedCert() async { + let now = self.now await self.assertChainFailsToMeetPolicy( chain: Self.chainWithSingleCertWithOCSP, requester: .noThrow { request, uri -> OCSPResponse in @@ -236,17 +243,18 @@ final class OCSPVerifierPolicyTests: XCTestCase { return .successful(.signed(responses: [OCSPSingleResponse( certID: singleRequest.certID, certStatus: .revoked(.init( - revocationTime: try .init(self.now), + revocationTime: try .init(now), revocationReason: .unspecified )), - thisUpdate: try .init(self.now - .days(1)), - nextUpdate: try .init(self.now + .days(1)) + thisUpdate: try .init(now - .days(1)), + nextUpdate: try .init(now + .days(1)) )], responseExtensions: { nonce })) } ) } func testResponseDoesNotIncludeResponseForRequestedCert() async { + let now = self.now await self.assertChainFailsToMeetPolicy( chain: Self.chainWithSingleCertWithOCSP, requester: .noThrow { request, uri -> OCSPResponse in @@ -262,8 +270,8 @@ final class OCSPVerifierPolicyTests: XCTestCase { serialNumber: .init() ), certStatus: .good, - thisUpdate: try .init(self.now - .days(1)), - nextUpdate: try .init(self.now + .days(1)) + thisUpdate: try .init(now - .days(1)), + nextUpdate: try .init(now + .days(1)) )], responseExtensions: { nonce })) } )