ASN1Parser

main

Minimalistic Swift parser for ASN.1 data. Currently supports DER and therefore also BER binary format
DominikHorn/ASN1Parser

ASN1Parser

tests coverage

Safety first ASN.1 parsing in Swift.

Documentation

You may import ASN1Parser.docc into Xcode, open the prebuilt static site found in docs/documentation/asn1parser/index.html, preview locally using preview-doc.sh or build the static website on your own using gen-doc.sh.

Note that the static site is sadly not hostable via github pages due to its case insensitive links which github pages routes as 404.

Quick Start

Given the a DER encoded ASN.1 representation of a public key, for example as found in .pem files:

// Public key data blob from pem file
let pemBlob = "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEQPtmXeh4gkzq30Zq3LXdgcl39fgCOBRZExhNWgZTSv5NTvbRoZNx28Ln/+Wtkfc42nWdunurluAeMPr0BrnLtA=="
guard let derData = Data(base64Encoded: pemBlob) else { return }

// Parse DER into ASN.1 value, which is a tree like structure
let tree = try DERParser.parse(derData)

// Access values within the tree
let q = try tree.asSequence[1].asBitString

Feature Overview

  • runtime safe ASN.1 parsing - error handling enforced by the swift compiler
  • minimalistic - only contains the features you need
  • quick and easy to use - immediately start parsing DER or BER encoded ASN.1 without any boilerplate
  • implicit ASN.1 format specification - no need worry about explicitely specifying the expected data format. Simply access values in the ASN.1 tree and dynamically receive errors if they are not available.

Please note that the choice of implicit format specification is a deliberate one. I believe ASN.1 is not particularily suited to be 1:1 mapped to swift types. Therefore you will most likely have to introduce an abstraction layer, i.e., an explicit ASN.1 format specification in addition to the swift type defintion you'd ideally want to use throughout your remaining code. This abstraction layer is however unnecessary IMHO, as it only consists of boilerplate definitions and code for converting between the ASN.1 format and your swift type. In addition to the often wasteful and messy nature of boilerplate code, having two different abstractions for the same underlying concept can introduce inconsistencies and thus bugs.

With implicit ASN.1 format specification, you can access ASN.1 data safely and quicker while not having having to worry about introducing any accidental inconsistencies in your codebase, because an additional abstraction layer won't exist. Simply define your desired type in swift and dynamically load its values from the decoded ASN.1 representation.

Contributing

Pull requests are of course are highly appreciated :)

Description

  • Swift Tools 5.5.0
View More Packages from this Author

Dependencies

Last updated: Tue Mar 19 2024 06:47:44 GMT-0900 (Hawaii-Aleutian Daylight Time)