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.
This commit is contained in:
emaloney authored and GitHub committed 2026-08-27 20:07:04 +01:00
1 parent 449dbbecd0
commit c8aece90ea
2 files changed
+63 -2

No files matched your search

@@ -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
)
}
@@ -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<ASN1Object: DERParseable & DERSerializable & Equatable>(
_ 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]
)
)
}
}