mirror of
https://github.com/apple/swift-certificates
synced 2026-11-22 04:20:39 +00:00
Merge branch 'main' into dn-ocsp-flaky-tests
This commit is contained in:
17 files changed
+55
-62
No files matched your search
+2
-2
@@ -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: [
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
@preconcurrency import Crypto
|
||||
@preconcurrency import _CryptoExtras
|
||||
import Foundation
|
||||
|
||||
extension Certificate {
|
||||
/// A private key that can be used with a certificate.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,8 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import SwiftASN1
|
||||
|
||||
/// Set the Common Name (CN) of a ``DistinguishedName``.
|
||||
///
|
||||
/// This type is used in ``DistinguishedNameBuilder`` contexts.
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import SwiftASN1
|
||||
|
||||
/// Set the Country Name (C) of a ``DistinguishedName``.
|
||||
///
|
||||
/// This type is used in ``DistinguishedNameBuilder`` contexts.
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import SwiftASN1
|
||||
|
||||
/// Set the Locality Name (L) of a ``DistinguishedName``.
|
||||
///
|
||||
/// This type is used in ``DistinguishedNameBuilder`` contexts.
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import SwiftASN1
|
||||
|
||||
/// Set the Organization Name (O) of a ``DistinguishedName``.
|
||||
///
|
||||
/// This type is used in ``DistinguishedNameBuilder`` contexts.
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import SwiftASN1
|
||||
|
||||
/// Set the Organizational Unit Name (OU) of a ``DistinguishedName``.
|
||||
///
|
||||
/// This type is used in ``DistinguishedNameBuilder`` contexts.
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import SwiftASN1
|
||||
|
||||
/// Set the State or Province Name (ST) of a ``DistinguishedName``.
|
||||
///
|
||||
/// This type is used in ``DistinguishedNameBuilder`` contexts.
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import SwiftASN1
|
||||
|
||||
/// Set the Street Address (STREET) of a ``DistinguishedName``.
|
||||
///
|
||||
/// This type is used in ``DistinguishedNameBuilder`` contexts.
|
||||
|
||||
@@ -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<Requester: OCSPRequester>: VerifierPolicy {
|
||||
public struct OCSPVerifierPolicy<Requester: OCSPRequester>: 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<Result>(
|
||||
private func withTimeout<Result: Sendable>(
|
||||
_ maxDuration: TimeInterval,
|
||||
operation: @escaping @Sendable () async -> Result
|
||||
) async -> Result {
|
||||
|
||||
@@ -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<RelativeDistinguishedName.Attribute>()
|
||||
|
||||
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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 }))
|
||||
}
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user