From 987848d4e9ab7348fe0a266179612b740e9a63ee Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Thu, 9 Feb 2023 09:33:31 +0100 Subject: [PATCH 1/6] Properly support SET OF fields. --- Package.swift | 2 +- .../CMSSignedData.swift | 31 ++++--------------- Sources/X509/RelativeDistinguishedName.swift | 17 ++-------- 3 files changed, 9 insertions(+), 41 deletions(-) diff --git a/Package.swift b/Package.swift index 41816ec..d92c3d5 100644 --- a/Package.swift +++ b/Package.swift @@ -32,7 +32,7 @@ let package = Package( .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: "git@github.com:apple/swift-asn1.git", branch: "dn-set-of"), .package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"), ], targets: [ diff --git a/Sources/X509/CryptographicMessageSyntax/CMSSignedData.swift b/Sources/X509/CryptographicMessageSyntax/CMSSignedData.swift index c1db119..8ec177f 100644 --- a/Sources/X509/CryptographicMessageSyntax/CMSSignedData.swift +++ b/Sources/X509/CryptographicMessageSyntax/CMSSignedData.swift @@ -73,27 +73,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 +98,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/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 From f90ac834356ab5736667554da155c43414b8a520 Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Thu, 9 Feb 2023 10:06:27 +0100 Subject: [PATCH 2/6] Fix 5.8 warnings --- Sources/X509/CertificatePrivateKey.swift | 1 + Sources/X509/CertificatePublicKey.swift | 1 + .../DistinguishedNameBuilder/CommonName.swift | 2 ++ .../CountryName.swift | 2 ++ .../LocalityName.swift | 2 ++ .../OrganizationName.swift | 2 ++ .../OrganizationalUnitName.swift | 2 ++ .../StateOrProvinceName.swift | 2 ++ .../StreetAddress.swift | 2 ++ Sources/X509/OCSP/OCSPPolicy.swift | 4 ++-- Sources/X509/Signature.swift | 1 + Sources/X509/Verifier/VerifierPolicy.swift | 2 +- Tests/X509Tests/DistinguishedNameTests.swift | 2 +- Tests/X509Tests/OCSPPolicyVerifierTests.swift | 22 +++++++++++-------- 14 files changed, 34 insertions(+), 13 deletions(-) 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/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 d92d45f..9454c78 100644 --- a/Sources/X509/OCSP/OCSPPolicy.swift +++ b/Sources/X509/OCSP/OCSPPolicy.swift @@ -77,7 +77,7 @@ enum OCSPRequestHashAlgorithm { } } -public struct OCSPVerifierPolicy: VerifierPolicy { +public struct OCSPVerifierPolicy: VerifierPolicy, Sendable { private var requester: Requester private var requestHashAlgorithm: OCSPRequestHashAlgorithm @@ -303,7 +303,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/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 9252d69..1538982 100644 --- a/Tests/X509Tests/OCSPPolicyVerifierTests.swift +++ b/Tests/X509Tests/OCSPPolicyVerifierTests.swift @@ -171,6 +171,7 @@ final class OCSPVerifierPolicyTests: XCTestCase { } func testSingleCertWithOCSP() async { + let now = self.now await self.assertChainMeetsPolicy( chain: Self.chainWithSingleCertWithOCSP, requester: .noThrow { request, uri -> OCSPResponse in @@ -182,14 +183,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 @@ -201,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: { OCSPNonce() })) } ) } func testRevokedCert() async { + let now = self.now await self.assertChainFailsToMeetPolicy( chain: Self.chainWithSingleCertWithOCSP, requester: .noThrow { request, uri -> OCSPResponse in @@ -220,17 +223,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 @@ -246,8 +250,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 })) } ) From dd8109d6f1ebaf55a1cde46b7e30c0302c9e3ab4 Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Thu, 9 Feb 2023 10:21:48 +0100 Subject: [PATCH 3/6] =?UTF-8?q?Foundation=20on=20Linux=20hasn=E2=80=99t=20?= =?UTF-8?q?adopted=20Sendable=20yet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/apple/swift-corelibs-foundation/issues/4687 --- Sources/X509/OCSP/OCSPPolicy.swift | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Sources/X509/OCSP/OCSPPolicy.swift b/Sources/X509/OCSP/OCSPPolicy.swift index 9454c78..9db20fd 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. From 40e1da48993b7ec9552601b52e2c7f78b5a112ac Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Thu, 9 Feb 2023 10:41:39 +0100 Subject: [PATCH 4/6] @preconcurrency in tests as well --- Tests/X509Tests/OCSPPolicyVerifierTests.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Tests/X509Tests/OCSPPolicyVerifierTests.swift b/Tests/X509Tests/OCSPPolicyVerifierTests.swift index 1538982..cec2c33 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 From d56fa0890af77416124b9b260e06aef72a6f1482 Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Fri, 10 Feb 2023 11:58:53 +0100 Subject: [PATCH 5/6] use released version --- Package.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Package.swift b/Package.swift index d92c3d5..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", branch: "dn-set-of"), + // .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: [ From 0abe1f833033a51f629787a515e6628065104819 Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Fri, 10 Feb 2023 12:09:53 +0100 Subject: [PATCH 6/6] Remove unused errors --- Sources/X509/CryptographicMessageSyntax/CMSSignedData.swift | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Sources/X509/CryptographicMessageSyntax/CMSSignedData.swift b/Sources/X509/CryptographicMessageSyntax/CMSSignedData.swift index 8ec177f..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 }