Skip to content

Commit

Permalink
Tag CMS signer identifier implicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Kim committed Jan 13, 2025
1 parent 274f866 commit e58b688
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
23 changes: 10 additions & 13 deletions Sources/X509/CryptographicMessageSyntax/CMSSignerIdentifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import SwiftASN1
/// ```
@usableFromInline
enum CMSSignerIdentifier: DERParseable, BERParseable, DERSerializable, BERSerializable, Hashable, Sendable {

@usableFromInline
static let skiIdentifier = ASN1Identifier(tagWithNumber: 0, tagClass: .contextSpecific)

Expand All @@ -36,13 +35,14 @@ enum CMSSignerIdentifier: DERParseable, BERParseable, DERSerializable, BERSerial
self = try .issuerAndSerialNumber(.init(derEncoded: node))

case Self.skiIdentifier:
self = try DER.explicitlyTagged(
node,
tagNumber: Self.skiIdentifier.tagNumber,
tagClass: Self.skiIdentifier.tagClass
) { node in
.subjectKeyIdentifier(.init(keyIdentifier: try ASN1OctetString(derEncoded: node).bytes))
}
self = try .subjectKeyIdentifier(
.init(
keyIdentifier: ASN1OctetString(
derEncoded: node,
withIdentifier: Self.skiIdentifier
).bytes
)
)

default:
throw ASN1Error.unexpectedFieldType(node.identifier)
Expand All @@ -56,11 +56,8 @@ enum CMSSignerIdentifier: DERParseable, BERParseable, DERSerializable, BERSerial
try issuerAndSerialNumber.serialize(into: &coder)

case .subjectKeyIdentifier(let subjectKeyIdentifier):
try coder.serialize(
ASN1OctetString(contentBytes: subjectKeyIdentifier.keyIdentifier),
explicitlyTaggedWithIdentifier: Self.skiIdentifier
)

try ASN1OctetString(contentBytes: subjectKeyIdentifier.keyIdentifier)
.serialize(into: &coder, withIdentifier: Self.skiIdentifier)
}
}

Expand Down
13 changes: 13 additions & 0 deletions Tests/X509Tests/CMSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,19 @@ final class CMSTests: XCTestCase {
}
XCTAssertInvalidCMSBlock(isValidSignature)
}

func testSubjectKeyIdentifierIsCorrectlyImplicitylyTagged() throws {
let implicitlyTaggedSki: [UInt8] = [
0x80, // Context-specific tag [0]
0x04, // Length
0x0a, 0x14, 0x1e, 0x28
]

XCTAssertEqual(
try CMSSignerIdentifier(derEncoded: implicitlyTaggedSki),
CMSSignerIdentifier.subjectKeyIdentifier(.init(keyIdentifier: [10, 20, 30, 40]))
)
}
}

extension DERSerializable {
Expand Down

0 comments on commit e58b688

Please sign in to comment.