From c8aece90ea05f9866bd392a5bf13b5cae56c0e03 Mon Sep 17 00:00:00 2001 From: emaloney Date: Thu, 27 Aug 2026 15:07:04 -0400 Subject: [PATCH] Fix `CMSSignerInfo`'s `unsignedAttrs` tag (#307) ### Motivation `CMSSignerInfo` could not round-trip `unsignedAttrs`: it serializes them as `[1] IMPLICIT SET` but the DER and BER parsers asked for the inner set with implicit tag `[0]`, so parsing any SignerInfo with unsigned attributes threw `unexpectedFieldType: [1]`. ### Changes Fixes issue #306 by: - Correcting the `unsignedAttrs` inner set identifier from `[0]` to `[1]` in the DER and BER initializers of `CMSSignerInfo`. - Adding a round-trip unit test for a `CMSSignerInfo` carrying `unsignedAttrs`. ### Result `unsignedAttrs` now round-trip correctly; no behavior change to the (already correct) serialized output. --- .../CMSSignerInfo.swift | 4 +- ...rInfoUnsignedAttributesRoundTripTest.swift | 61 +++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 Tests/X509Tests/CMSSignerInfoUnsignedAttributesRoundTripTest.swift diff --git a/Sources/X509/CryptographicMessageSyntax/CMSSignerInfo.swift b/Sources/X509/CryptographicMessageSyntax/CMSSignerInfo.swift index 81fcd09..f94db5e 100644 --- a/Sources/X509/CryptographicMessageSyntax/CMSSignerInfo.swift +++ b/Sources/X509/CryptographicMessageSyntax/CMSSignerInfo.swift @@ -137,7 +137,7 @@ struct CMSSignerInfo: DERImplicitlyTaggable, BERImplicitlyTaggable, Hashable, Se node in return try DER.set( of: CMSAttribute.self, - identifier: .init(tagWithNumber: 0, tagClass: .contextSpecific), + identifier: .init(tagWithNumber: 1, tagClass: .contextSpecific), rootNode: node ) } @@ -192,7 +192,7 @@ struct CMSSignerInfo: DERImplicitlyTaggable, BERImplicitlyTaggable, Hashable, Se node in return try BER.set( of: CMSAttribute.self, - identifier: .init(tagWithNumber: 0, tagClass: .contextSpecific), + identifier: .init(tagWithNumber: 1, tagClass: .contextSpecific), rootNode: node ) } diff --git a/Tests/X509Tests/CMSSignerInfoUnsignedAttributesRoundTripTest.swift b/Tests/X509Tests/CMSSignerInfoUnsignedAttributesRoundTripTest.swift new file mode 100644 index 0000000..f0fe06a --- /dev/null +++ b/Tests/X509Tests/CMSSignerInfoUnsignedAttributesRoundTripTest.swift @@ -0,0 +1,61 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftCertificates open source project +// +// Copyright (c) 2026 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 XCTest +import SwiftASN1 +@testable @_spi(CMS) import X509 + +final class CMSSignerInfoUnsignedAttributesRoundTripTest: XCTestCase { + + private func assertRoundTrips( + _ value: ASN1Object + ) + throws + { + var serializer = DER.Serializer() + try serializer.serialize(value) + let parsed = try ASN1Object(derEncoded: serializer.serializedBytes) + XCTAssertEqual(parsed, value) + } + + func testCMSSignerInfoWithUnsignedAttrsRoundTrips() + throws + { + // A SignerInfo carrying unsignedAttrs must survive a serialize/parse round trip. + // This exercises the `[1] IMPLICIT` unsignedAttrs path. + let unsignedAttr = CMSAttribute( + attrType: .contentType, + attrValues: [try ASN1Any(erasing: ASN1OctetString(contentBytes: [0xDE, 0xAD, 0xBE, 0xEF]))] + ) + try assertRoundTrips( + CMSSignerInfo( + version: .v1, + signerIdentifier: .issuerAndSerialNumber( + .init( + issuer: .init { + CountryName("US") + OrganizationName("Apple Inc.") + CommonName("Apple Public EV Server ECC CA 1 - G1") + }, + serialNumber: .init(bytes: [20, 30, 40, 50]) + ) + ), + digestAlgorithm: .sha256WithRSAEncryptionUsingNil, + signatureAlgorithm: .ecdsaWithSHA256, + signature: .init(contentBytes: [100, 110, 120, 130, 140]), + unsignedAttrs: [unsignedAttr] + ) + ) + } +}