From 43bb669689d7eb3e7073254b20ddf89dd8beded1 Mon Sep 17 00:00:00 2001 From: 5HT Date: Tue, 1 Oct 2024 01:19:00 +0300 Subject: [PATCH] CHATMessage, identity exception --- Sources/Suite/ASN1SCG/CHATProtocol.swift | 7 ++-- Sources/Suite/main.swift | 43 +++++++++++++++++------- asn1.ex | 2 +- priv/basic/KEP.asn1 | 2 -- 4 files changed, 35 insertions(+), 19 deletions(-) diff --git a/Sources/Suite/ASN1SCG/CHATProtocol.swift b/Sources/Suite/ASN1SCG/CHATProtocol.swift index 9b72ccd..8611ce6 100644 --- a/Sources/Suite/ASN1SCG/CHATProtocol.swift +++ b/Sources/Suite/ASN1SCG/CHATProtocol.swift @@ -57,9 +57,10 @@ import Foundation @inlinable func serialize(into coder: inout DER.Serializer, withIdentifier: ASN1Identifier) throws { switch self { case .register(let register): - try coder.appendConstructedNode( - identifier: ASN1Identifier(tagWithNumber: 0, tagClass: .contextSpecific), - { coder in try coder.serialize(register) }) + try coder.serializeOptionalImplicitlyTagged(register, withIdentifier: ASN1Identifier(tagWithNumber: 0, tagClass: .contextSpecific)) + //try coder.appendConstructedNode( + //identifier: ASN1Identifier(tagWithNumber: 0, tagClass: .contextSpecific), + //{ coder in try coder.serialize(register, explicitlyTaggedWithIdentifier: withIdentifier) }) case .auth(let auth): try coder.appendConstructedNode( identifier: ASN1Identifier(tagWithNumber: 1, tagClass: .contextSpecific), diff --git a/Sources/Suite/main.swift b/Sources/Suite/main.swift index f1b8bc6..224e4fe 100644 --- a/Sources/Suite/main.swift +++ b/Sources/Suite/main.swift @@ -3,52 +3,63 @@ import Foundation exit(Console.suite()) +extension String: Error { } + public class Console { public static func exists(f: String) -> Bool { return FileManager.default.fileExists(atPath: f) } public static func showName(data: Array) throws { let name: Name? = try Name(derEncoded: data) - if let name { print(": Name \(name)") } var serializer = DER.Serializer() try name!.serialize(into: &serializer) print(": Name.DER \(data)") - print(": DER.Name \(serializer.serializedBytes)") + print(": Name ⟼ \(name!)\n") + if (data != serializer.serializedBytes) { throw "DER <-> Name lacks equality properties." } } public static func showGeneralName(data: Array) throws { let name: GeneralName? = try GeneralName(derEncoded: data) - if let name { print(": GeneralName \(name)") } var serializer = DER.Serializer() try name!.serialize(into: &serializer) print(": GeneralName.DER \(data)") - print(": DER.GeneralName \(serializer.serializedBytes)") + print(": GeneralName ⟼ \(name!)\n") + if (data != serializer.serializedBytes) { throw "DER <-> GeneralName lacks equality properties." } } public static func showDirectoryString(data: Array) throws { let ds: DirectoryString? = try DirectoryString(derEncoded: data) - if let ds { print(": DirectoryString \(ds)") } var serializer = DER.Serializer() try ds!.serialize(into: &serializer) print(": DirectoryString.DER \(data)") - print(": DER.DirectoryString \(serializer.serializedBytes)") + print(": DirectoryString ⟼ \(ds!)\n") + if (data != serializer.serializedBytes) { throw "DER <-> DirectoryString lacks equality properties." } } public static func showLDAPMessage(data: Array) throws { let msg: LDAPMessage? = try LDAPMessage(derEncoded: data) - if let msg { print(": LDAPMessage \(msg)") } var serializer = DER.Serializer() try msg!.serialize(into: &serializer) print(": LDAPMessage.DER \(data)") - print(": DER.LDAPMessage \(serializer.serializedBytes)") + print(": LDAPMessage ⟼ \(msg!)\n") + if (data != serializer.serializedBytes) { throw "DER <-> LDAPMessage lacks equality properties." } + } + + public static func showCHATMessage(data: Array) throws { + let msg: CHATMessage? = try CHATMessage(derEncoded: data) + var serializer = DER.Serializer() + try msg!.serialize(into: &serializer) + print(": CHATMessage.DER \(data)") + print(": CHATMessage ⟼ \(msg!)\n") + if (data != serializer.serializedBytes) { throw "DER <-> CHATMessage lacks equality properties." } } public static func showCertificate(file: String) throws { let url = URL(fileURLWithPath: file) if (!Console.exists(f: url.path)) { print(": CERT file not found.") } else { let data = try Data(contentsOf: url) - let cert = try Certificate(derEncoded: Array(data)) - print(": Certificate \(cert)") + let cert = try Certificate(derEncoded: Array(data)) // display TBSCertificate envelop from DSTU.asn1 + print(": Certificate ⟼ \(cert)\n") } } @@ -59,10 +70,15 @@ public class Console { var cert = try ContentInfo(derEncoded: Array(data)) var serializer = DER.Serializer() try cert.content.serialize(into: &serializer) - let signedData = try SignedData(derEncoded: Array(serializer.serializedBytes)) - print(": SignedData \(signedData)") + var signedData = try SignedData(derEncoded: Array(serializer.serializedBytes)) + let content: String? = try String(bytes: signedData.encapContentInfo.eContent!.bytes, encoding: .utf8) cert.content = try ASN1Any(erasing: ASN1Null()) - print(": ContentInfo \(cert)") + signedData.encapContentInfo.eContent = nil + print(": SignedData ⟼ \(signedData)\n ") // display SignedData envelope from KEP.asn1 + print(": signedData.encapContentInfo.eContent := \(content!)\n") // display signed content + + print(": ContentInfo.DER \(data)") + print(": ContentInfo ⟼ \(cert)\n") } } @@ -72,6 +88,7 @@ public class Console { try showContentInfo(file: "data.bin") try showDirectoryString(data: [19,3,49,50,51]) try showLDAPMessage(data: [48,16,2,1,1,96,9,2,1,1,4,0,128,2,49,50,160,0]) + try showCHATMessage(data: [48,27,2,1,1,48,0,160,20,4,3,53,72,84,4,7,53,72,84,46,99,115,114,4,4,48,48,48,48]) try showName(data: [48,13,49,11,48,9,6,3,85,4,6,19,2,85,65]) try showName(data: [48,0]) try showGeneralName(data: [164,2,48,0]) diff --git a/asn1.ex b/asn1.ex index 59f678d..f36afd6 100755 --- a/asn1.ex +++ b/asn1.ex @@ -672,6 +672,6 @@ case System.argv() do ["compile",i,o] -> ASN1.setEnv(:input, i <> "/") ; ASN1.setEnv(:output, o <> "/") ; ASN1.compile ["compile","-v",i,o] -> ASN1.setEnv(:input, i <> "/") ; ASN1.setEnv(:output, o <> "/") ; ASN1.setEnv(:verbose, true) ; ASN1.compile _ -> :io.format('Copyright © 1994—2024 Namdak Tönpa.~n') - :io.format('ISO 8824 ITU/IETF X.680-690 ERP/1 ASN.1 DER Compiler, version 30.9.27.~n') + :io.format('ISO 8824 ITU/IETF X.680-690 ERP/1 ASN.1 DER Compiler, version 30.10.1.~n') :io.format('Usage: ./asn1.ex help | compile [-v] [input [output]]~n') end diff --git a/priv/basic/KEP.asn1 b/priv/basic/KEP.asn1 index 51b331d..508e8b8 100644 --- a/priv/basic/KEP.asn1 +++ b/priv/basic/KEP.asn1 @@ -2,8 +2,6 @@ -- https://zakon.rada.gov.ua/laws/show/z1401-12 -- https://zakon.rada.gov.ua/laws/show/z1402-12 --- https://zakon.rada.gov.ua/laws/show/z0607-17 --- https://zakon.rada.gov.ua/laws/show/z2230-13 -- https://zakon.rada.gov.ua/laws/show/z1398-12 -- PKCS + OCSP + TSP + Authentication + Information (X.501:08, X.509:08, X.511:08, X.520:08).