Skip to content

Commit

Permalink
CHATMessage, identity exception
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT committed Sep 30, 2024
1 parent af8b798 commit 43bb669
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
7 changes: 4 additions & 3 deletions Sources/Suite/ASN1SCG/CHATProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
43 changes: 30 additions & 13 deletions Sources/Suite/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<UInt8>) 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<UInt8>) 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<UInt8>) 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<UInt8>) 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<UInt8>) 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")
}
}

Expand All @@ -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")
}
}

Expand All @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion asn1.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 0 additions & 2 deletions priv/basic/KEP.asn1
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down

0 comments on commit 43bb669

Please sign in to comment.