Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT committed Apr 13, 2024
1 parent 78a35b8 commit 2be77a5
Show file tree
Hide file tree
Showing 18 changed files with 74 additions and 101 deletions.
9 changes: 7 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ let package = Package(
name: "chat-asn1",
platforms: [ .macOS(.v12), .iOS(.v13) ],
products: [ .library(name: "chat-asn1", targets: ["ASN1SCG"]), ],
targets: [ .target(name: "ASN1SCG", dependencies: [ .product(name: "SwiftASN1", package: "swift-asn1"), ]), ]
targets: [ .target(name: "ASN1SCG", dependencies: [
.product(name: "Crypto", package: "swift-crypto"),
.product(name: "_CryptoExtras", package: "swift-crypto"),
.product(name: "SwiftASN1", package: "swift-asn1"), ]), ]
)

if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
package.dependencies += [ .package(url: "https://github.com/apple/swift-asn1.git", from: "0.8.0"), ]
package.dependencies += [
.package(url: "https://github.com/apple/swift-crypto.git", from: "2.6.0"),
.package(url: "https://github.com/apple/swift-asn1.git", from: "0.8.0"), ]
}
6 changes: 3 additions & 3 deletions Sources/ASN1SCG/AttCertIssuer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import Foundation
case v2Form(V2Form)
@inlinable init(derEncoded rootNode: ASN1Node) throws {
switch rootNode.identifier {
case ASN1Identifier.sequence:
self = .v1Form(try DER.sequence(of: GeneralName.self, identifier: rootNode.identifier, rootNode: rootNode))
case [GeneralName].defaultIdentifier:
self = .v1Form(try [GeneralName](derEncoded: rootNode))
case ASN1Identifier(tagWithNumber: 0, tagClass: .contextSpecific):
self = .v2Form(try V2Form(derEncoded: rootNode))
default: throw ASN1Error.unexpectedFieldType(rootNode.identifier)
}
}
@inlinable func serialize(into coder: inout DER.Serializer) throws {
switch self {
case .v1Form(let v1Form): try coder.serializeSequenceOf(v1Form)
case .v1Form(let v1Form): try coder.serialize(v1Form)
case .v2Form(let v2Form):
try coder.appendConstructedNode(
identifier: ASN1Identifier(tagWithNumber: 0, tagClass: .contextSpecific),
Expand Down
6 changes: 3 additions & 3 deletions Sources/ASN1SCG/Attribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import Foundation
@usableFromInline struct Attribute: DERImplicitlyTaggable, Hashable, Sendable {
@inlinable static var defaultIdentifier: ASN1Identifier { .sequence }
@usableFromInline var type: ASN1ObjectIdentifier
@usableFromInline var values: [ASN1OctetString]
@inlinable init(type: ASN1ObjectIdentifier, values: [ASN1OctetString]) {
@usableFromInline var values: [ASN1Any]
@inlinable init(type: ASN1ObjectIdentifier, values: [ASN1Any]) {
self.type = type
self.values = values
}
@inlinable init(derEncoded root: ASN1Node,
withIdentifier identifier: ASN1Identifier) throws {
self = try DER.sequence(root, identifier: identifier) { nodes in
let type: ASN1ObjectIdentifier = try ASN1ObjectIdentifier(derEncoded: &nodes)
let values: [ASN1OctetString] = try DER.set(of: ASN1OctetString.self, identifier: .set, nodes: &nodes)
let values: [ASN1Any] = try DER.set(of: ASN1Any.self, identifier: .set, nodes: &nodes)
return Attribute(type: type, values: values)
}
}
Expand Down
13 changes: 7 additions & 6 deletions Sources/ASN1SCG/ContentInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import Foundation

@usableFromInline struct ContentInfo: DERImplicitlyTaggable, Hashable, Sendable {
@inlinable static var defaultIdentifier: ASN1Identifier { .sequence }
@usableFromInline var contentType: ASN1ObjectIdentifier
@usableFromInline var content: ASN1Any
@inlinable init(contentType: ASN1ObjectIdentifier, content: ASN1Any) {
@usableFromInline var contentType: id
@usableFromInline var content: Type
@inlinable init(contentType: id, content: Type) {
self.contentType = contentType
self.content = content
}
@inlinable init(derEncoded root: ASN1Node, withIdentifier identifier: ASN1Identifier) throws {
@inlinable init(derEncoded root: ASN1Node,
withIdentifier identifier: ASN1Identifier) throws {
self = try DER.sequence(root, identifier: identifier) { nodes in
let contentType: ASN1ObjectIdentifier = try ASN1ObjectIdentifier(derEncoded: &nodes)
let content: ASN1Any = try DER.explicitlyTagged(&nodes, tagNumber: 0, tagClass: .contextSpecific) { node in return try ASN1Any(derEncoded: node) }
let contentType: id = try id(derEncoded: &nodes)
let content: Type = try DER.explicitlyTagged(&nodes, tagNumber: 0, tagClass: .contextSpecific) { node in return try Type(derEncoded: node) }
return ContentInfo(contentType: contentType, content: content)
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/ASN1SCG/DisplayText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import Foundation

@usableFromInline indirect enum DisplayText: DERParseable, DERSerializable, Hashable, Sendable {
case ia5String(ASN1IA5String)
case visibleString(ASN1UTF8String)
case visibleString(VisibleString)
case bmpString(ASN1BMPString)
case utf8String(ASN1UTF8String)
@inlinable init(derEncoded rootNode: ASN1Node) throws {
switch rootNode.identifier {
case ASN1IA5String.defaultIdentifier:
self = .ia5String(try ASN1IA5String(derEncoded: rootNode))
case ASN1Identifier.visibleString:
self = .visibleString(try ASN1UTF8String(derEncoded: rootNode))
case VisibleString.defaultIdentifier:
self = .visibleString(try VisibleString(derEncoded: rootNode))
case ASN1BMPString.defaultIdentifier:
self = .bmpString(try ASN1BMPString(derEncoded: rootNode))
case ASN1UTF8String.defaultIdentifier:
Expand Down
9 changes: 2 additions & 7 deletions Sources/ASN1SCG/DistributionPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import Foundation
@inlinable init(derEncoded root: ASN1Node,
withIdentifier identifier: ASN1Identifier) throws {
self = try DER.sequence(root, identifier: identifier) { nodes in
nodes.next()
let distributionPoint: DistributionPointName? = try DistributionPointName(derEncoded: &nodes)
let distributionPoint: DistributionPointName? = try DER.optionalImplicitlyTagged(&nodes, tag: ASN1Identifier(tagWithNumber: 0, tagClass: .contextSpecific))
let reasons: ASN1BitString? = try DER.optionalImplicitlyTagged(&nodes, tag: ASN1Identifier(tagWithNumber: 1, tagClass: .contextSpecific))
let cRLIssuer: [GeneralName] = try DER.sequence(of: GeneralName.self, identifier: ASN1Identifier(tagWithNumber: 2, tagClass: .contextSpecific), nodes: &nodes)
return DistributionPoint(distributionPoint: distributionPoint, reasons: reasons, cRLIssuer: cRLIssuer)
Expand All @@ -26,11 +25,7 @@ import Foundation
@inlinable func serialize(into coder: inout DER.Serializer,
withIdentifier identifier: ASN1Identifier) throws {
try coder.appendConstructedNode(identifier: identifier) { coder in
if let distributionPoint = self.distributionPoint {
try coder.appendConstructedNode(
identifier: ASN1Identifier(tagWithNumber: 0, tagClass: .contextSpecific),
{ coder in try coder.serialize(distributionPoint) })
}
if let distributionPoint = self.distributionPoint { try coder.serializeOptionalImplicitlyTagged(distributionPoint, withIdentifier: ASN1Identifier(tagWithNumber: 0, tagClass: .contextSpecific)) }
if let reasons = self.reasons { try coder.serializeOptionalImplicitlyTagged(reasons, withIdentifier: ASN1Identifier(tagWithNumber: 1, tagClass: .contextSpecific)) }
if let cRLIssuer = self.cRLIssuer { try coder.serializeSequenceOf(cRLIssuer, identifier: ASN1Identifier(tagWithNumber: 2, tagClass: .contextSpecific)) }
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/ASN1SCG/DistributionPointName.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import Foundation
@inlinable init(derEncoded rootNode: ASN1Node) throws {
switch rootNode.identifier {
case ASN1Identifier(tagWithNumber: 0, tagClass: .contextSpecific):
self = .fullName(try DER.sequence(of: GeneralName.self, identifier: rootNode.identifier, rootNode: rootNode))
self = .fullName(try [GeneralName](derEncoded: rootNode))
case ASN1Identifier(tagWithNumber: 1, tagClass: .contextSpecific):
self = .nameRelativeToCRLIssuer(try DER.sequence(of: AttributeTypeAndValue.self, identifier: rootNode.identifier, rootNode: rootNode))
self = .nameRelativeToCRLIssuer(try [AttributeTypeAndValue](derEncoded: rootNode))
default: throw ASN1Error.unexpectedFieldType(rootNode.identifier)
}
}
Expand All @@ -20,11 +20,11 @@ import Foundation
case .fullName(let fullName):
try coder.appendConstructedNode(
identifier: ASN1Identifier(tagWithNumber: 0, tagClass: .contextSpecific),
{ coder in try coder.serializeSequenceOf(fullName) })
{ coder in try coder.serialize(fullName) })
case .nameRelativeToCRLIssuer(let nameRelativeToCRLIssuer):
try coder.appendConstructedNode(
identifier: ASN1Identifier(tagWithNumber: 1, tagClass: .contextSpecific),
{ coder in try coder.serializeSequenceOf(nameRelativeToCRLIssuer) })
{ coder in try coder.serialize(nameRelativeToCRLIssuer) })
}
}

Expand Down
18 changes: 4 additions & 14 deletions Sources/ASN1SCG/EDIPartyName.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,16 @@ import Foundation
@inlinable init(derEncoded root: ASN1Node,
withIdentifier identifier: ASN1Identifier) throws {
self = try DER.sequence(root, identifier: identifier) { nodes in
nodes.next()
let nameAssigner: DirectoryString? = try DirectoryString(derEncoded: &nodes)
nodes.next()
let partyName: DirectoryString = try DirectoryString(derEncoded: &nodes)
let nameAssigner: DirectoryString? = try DER.optionalImplicitlyTagged(&nodes, tag: ASN1Identifier(tagWithNumber: 0, tagClass: .contextSpecific))
let partyName: DirectoryString = (try DER.optionalImplicitlyTagged(&nodes, tag: ASN1Identifier(tagWithNumber: 1, tagClass: .contextSpecific)))!
return EDIPartyName(nameAssigner: nameAssigner, partyName: partyName)
}
}
@inlinable func serialize(into coder: inout DER.Serializer,
withIdentifier identifier: ASN1Identifier) throws {
try coder.appendConstructedNode(identifier: identifier) { coder in
if let nameAssigner = self.nameAssigner {
try coder.appendConstructedNode(
identifier: ASN1Identifier(tagWithNumber: 0, tagClass: .contextSpecific),
{ coder in try coder.serialize(nameAssigner) })
}
if let partyName = self.nameAssigner {
try coder.appendConstructedNode(
identifier: ASN1Identifier(tagWithNumber: 0, tagClass: .contextSpecific),
{ coder in try coder.serialize(partyName) })
}
if let nameAssigner = self.nameAssigner { try coder.serializeOptionalImplicitlyTagged(nameAssigner, withIdentifier: ASN1Identifier(tagWithNumber: 0, tagClass: .contextSpecific)) }
try coder.serializeOptionalImplicitlyTagged(partyName, withIdentifier: ASN1Identifier(tagWithNumber: 1, tagClass: .contextSpecific))
}
}
}
14 changes: 2 additions & 12 deletions Sources/ASN1SCG/K.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,18 @@ import Foundation
@usableFromInline var version: K_version_IntEnum
@usableFromInline var x: ArraySlice<UInt8>
@usableFromInline var y: K_y_Sequence
@usableFromInline var w: [[ArraySlice<UInt8>]]
@inlinable init(version: K_version_IntEnum, x: ArraySlice<UInt8>, y: K_y_Sequence, w: [[ArraySlice<UInt8>]]) {
@inlinable init(version: K_version_IntEnum, x: ArraySlice<UInt8>, y: K_y_Sequence) {
self.version = version
self.x = x
self.y = y
self.w = w
}
@inlinable init(derEncoded root: ASN1Node,
withIdentifier identifier: ASN1Identifier) throws {
self = try DER.sequence(root, identifier: identifier) { nodes in
let version = try K_version_IntEnum(rawValue: Int(derEncoded: &nodes))
let x: ArraySlice<UInt8> = try ArraySlice<UInt8>(derEncoded: &nodes)
let y: K_y_Sequence = try K_y_Sequence(derEncoded: &nodes)
let w: [[ArraySlice<UInt8>]] = try DER.set<[[ArraySlice<UInt8>]]>(nodes.next()!, identifier: .set) { nodes1 in
var wAcc: [[ArraySlice<UInt8>]] = []
while let wInner = nodes1.next() {
wAcc.append(try DER.sequence(of: ArraySlice<UInt8>.self, identifier: .sequence, rootNode: wInner))
}
return wAcc
}
return K(version: version, x: x, y: y, w: w)
return K(version: version, x: x, y: y)
}
}
@inlinable func serialize(into coder: inout DER.Serializer,
Expand All @@ -37,7 +28,6 @@ import Foundation
try coder.serialize(version.rawValue)
try coder.serialize(x)
try coder.serialize(y)
try coder.appendConstructedNode(identifier: ASN1Identifier.set) { codec in for element in w { try codec.serializeSequenceOf(element) } }
}
}
}
24 changes: 7 additions & 17 deletions Sources/ASN1SCG/Name.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,17 @@ import Foundation

@usableFromInline indirect enum Name: DERParseable, DERSerializable, Hashable, Sendable {
case rdnSequence([[AttributeTypeAndValue]])
@inlinable init(derEncoded root: ASN1Node) throws {
switch root.identifier {
case .sequence:
self = .rdnSequence(try DER.sequence<[[AttributeTypeAndValue]]>(root, identifier: .sequence) { nodes in
var rdnSequenceAcc: [[AttributeTypeAndValue]] = []
while let rdnSequenceInner = nodes.next() {
rdnSequenceAcc.append(try DER.sequence(of: AttributeTypeAndValue.self, identifier: .sequence, rootNode: rdnSequenceInner))
}
return rdnSequenceAcc
})
default: throw ASN1Error.unexpectedFieldType(root.identifier)
@inlinable init(derEncoded rootNode: ASN1Node) throws {
switch rootNode.identifier {
case [[AttributeTypeAndValue]].defaultIdentifier:
self = .rdnSequence(try [[AttributeTypeAndValue]](derEncoded: rootNode))
default: throw ASN1Error.unexpectedFieldType(rootNode.identifier)
}
}
@inlinable func serialize(into coder: inout DER.Serializer) throws {
switch self {
case .rdnSequence(let rdnSequence):
try coder.appendConstructedNode(identifier: ASN1Identifier.sequence) { codec in
for element in rdnSequence {
try codec.serializeSequenceOf(element)
}
}
case .rdnSequence(let rdnSequence): try coder.serialize(rdnSequence)
}
}

}
10 changes: 5 additions & 5 deletions Sources/ASN1SCG/OtherCertificateFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import Foundation

@usableFromInline struct OtherCertificateFormat: DERImplicitlyTaggable, Hashable, Sendable {
@inlinable static var defaultIdentifier: ASN1Identifier { .sequence }
@usableFromInline var otherCertFormat: ASN1ObjectIdentifier
@usableFromInline var otherCert: ASN1Any
@inlinable init(otherCertFormat: ASN1ObjectIdentifier, otherCert: ASN1Any) {
@usableFromInline var otherCertFormat: id
@usableFromInline var otherCert: Type
@inlinable init(otherCertFormat: id, otherCert: Type) {
self.otherCertFormat = otherCertFormat
self.otherCert = otherCert
}
@inlinable init(derEncoded root: ASN1Node,
withIdentifier identifier: ASN1Identifier) throws {
self = try DER.sequence(root, identifier: identifier) { nodes in
let otherCertFormat: ASN1ObjectIdentifier = try ASN1ObjectIdentifier(derEncoded: &nodes)
let otherCert: ASN1Any = try ASN1Any(derEncoded: &nodes)
let otherCertFormat: id = try id(derEncoded: &nodes)
let otherCert: Type = try Type(derEncoded: &nodes)
return OtherCertificateFormat(otherCertFormat: otherCertFormat, otherCert: otherCert)
}
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/ASN1SCG/OtherKeyAttribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import Foundation

@usableFromInline struct OtherKeyAttribute: DERImplicitlyTaggable, Hashable, Sendable {
@inlinable static var defaultIdentifier: ASN1Identifier { .sequence }
@usableFromInline var keyAttrId: ASN1ObjectIdentifier
@usableFromInline var keyAttr: ASN1Any
@inlinable init(keyAttrId: ASN1ObjectIdentifier, keyAttr: ASN1Any) {
@usableFromInline var keyAttrId: id
@usableFromInline var keyAttr: Type
@inlinable init(keyAttrId: id, keyAttr: Type) {
self.keyAttrId = keyAttrId
self.keyAttr = keyAttr
}
@inlinable init(derEncoded root: ASN1Node,
withIdentifier identifier: ASN1Identifier) throws {
self = try DER.sequence(root, identifier: identifier) { nodes in
let keyAttrId: ASN1ObjectIdentifier = try ASN1ObjectIdentifier(derEncoded: &nodes)
let keyAttr: ASN1Any = try ASN1Any(derEncoded: &nodes)
let keyAttrId: id = try id(derEncoded: &nodes)
let keyAttr: Type = try Type(derEncoded: &nodes)
return OtherKeyAttribute(keyAttrId: keyAttrId, keyAttr: keyAttr)
}
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/ASN1SCG/OtherRecipientInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import Foundation

@usableFromInline struct OtherRecipientInfo: DERImplicitlyTaggable, Hashable, Sendable {
@inlinable static var defaultIdentifier: ASN1Identifier { .sequence }
@usableFromInline var oriType: ASN1ObjectIdentifier
@usableFromInline var oriValue: ASN1Any
@inlinable init(oriType: ASN1ObjectIdentifier, oriValue: ASN1Any) {
@usableFromInline var oriType: id
@usableFromInline var oriValue: Type
@inlinable init(oriType: id, oriValue: Type) {
self.oriType = oriType
self.oriValue = oriValue
}
@inlinable init(derEncoded root: ASN1Node,
withIdentifier identifier: ASN1Identifier) throws {
self = try DER.sequence(root, identifier: identifier) { nodes in
let oriType: ASN1ObjectIdentifier = try ASN1ObjectIdentifier(derEncoded: &nodes)
let oriValue: ASN1Any = try ASN1Any(derEncoded: &nodes)
let oriType: id = try id(derEncoded: &nodes)
let oriValue: Type = try Type(derEncoded: &nodes)
return OtherRecipientInfo(oriType: oriType, oriValue: oriValue)
}
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/ASN1SCG/OtherRevocationInfoFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import Foundation

@usableFromInline struct OtherRevocationInfoFormat: DERImplicitlyTaggable, Hashable, Sendable {
@inlinable static var defaultIdentifier: ASN1Identifier { .sequence }
@usableFromInline var otherRevInfoFormat: ASN1ObjectIdentifier
@usableFromInline var otherRevInfo: ASN1Any
@inlinable init(otherRevInfoFormat: ASN1ObjectIdentifier, otherRevInfo: ASN1Any) {
@usableFromInline var otherRevInfoFormat: id
@usableFromInline var otherRevInfo: Type
@inlinable init(otherRevInfoFormat: id, otherRevInfo: Type) {
self.otherRevInfoFormat = otherRevInfoFormat
self.otherRevInfo = otherRevInfo
}
@inlinable init(derEncoded root: ASN1Node,
withIdentifier identifier: ASN1Identifier) throws {
self = try DER.sequence(root, identifier: identifier) { nodes in
let otherRevInfoFormat: ASN1ObjectIdentifier = try ASN1ObjectIdentifier(derEncoded: &nodes)
let otherRevInfo: ASN1Any = try ASN1Any(derEncoded: &nodes)
let otherRevInfoFormat: id = try id(derEncoded: &nodes)
let otherRevInfo: Type = try Type(derEncoded: &nodes)
return OtherRevocationInfoFormat(otherRevInfoFormat: otherRevInfoFormat, otherRevInfo: otherRevInfo)
}
}
Expand Down
Loading

0 comments on commit 2be77a5

Please sign in to comment.