Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse ASN.1 with Haskell?

Tags:

haskell

asn.1

How do I parse ASN.1 input with Haskell?

Is there a generator that is able to generate algebraic datatype declarations and parser code for a given ASN.1 description file?

(like e.g. asn1c for generating C code ...)

A quick summary what ASN.1 is about:

ASN.1 is like Google Protocol Buffers, but is was developed way earlier and it is an actual standard. Basically, ASN.1 defines several methods for serializing hierarchically structured data and a syntax for defining a grammar that describes the structure. Such a grammar can be used to automatically generate a parser and data structures for building a syntax tree.

like image 479
maxschlepzig Avatar asked Oct 18 '12 18:10

maxschlepzig


Video Answer


1 Answers

You can decode binary ASN.1 using the asn1-encoding package, which you can parse either by hand using pattern matching, or the asn1-parse package.

For example the following helper function will decode a BER binary file into a list of ASN1:

import Data.ASN1.Encoding
import Data.ASN1.BinaryEncoding
import Data.ASN1.Types
import qualified Data.ByteString as B

decodeASN1File file = decodeASN1' BER `fmap` B.readFile file
like image 137
Tab Avatar answered Oct 08 '22 13:10

Tab