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


tests coverage

Safety first ASN.1 parsing in Swift.


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

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.


Pull requests are of course are highly appreciated :)


  • Swift Tools 5.5.0
View More Packages from this Author


Last updated: Fri Nov 11 2022 15:29:56 GMT-0500 (GMT-05:00)