2020-04-21 14:21:36 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module Main where
|
|
|
|
|
|
|
|
import qualified Data.ASN1.BinaryEncoding as ASN1
|
2020-05-19 12:29:15 +02:00
|
|
|
import qualified Data.ASN1.Encoding as ASN1
|
|
|
|
import qualified Data.ASN1.Error as ASN1
|
|
|
|
import qualified Data.ASN1.Parse as ASN1P
|
|
|
|
import qualified Data.ASN1.Types as ASN1
|
|
|
|
import qualified Data.ByteString as BS
|
|
|
|
import Data.Maybe (fromMaybe)
|
|
|
|
import Debug.Trace (trace)
|
2020-04-21 14:21:36 +02:00
|
|
|
|
|
|
|
-- import Hash2Pub.Fedichord
|
|
|
|
|
|
|
|
-- encoding values as ASN.1 types is done using Data.ASN1.Prim
|
2020-04-26 17:40:25 +02:00
|
|
|
someASN1 :: [ASN1.ASN1]
|
2020-05-19 12:29:15 +02:00
|
|
|
someASN1 = ASN1.Start ASN1.Sequence : ASN1.ASN1String (ASN1.asn1CharacterString ASN1.Visible domain) : ASN1.ASN1String (ASN1.asn1CharacterString ASN1.UTF8 unicode) : fmap ASN1.IntVal xs <> [ASN1.End ASN1.Sequence]
|
2020-04-21 14:21:36 +02:00
|
|
|
where
|
|
|
|
domain = "domains.are.ascii.on.ly"
|
|
|
|
unicode = "Hähä, but unicode string!"
|
|
|
|
xs = [ 23, 42, 2342 ]
|
|
|
|
|
2020-04-26 17:40:25 +02:00
|
|
|
asn1AsDer :: [ASN1.ASN1] -> BS.ByteString
|
|
|
|
asn1AsDer = ASN1.encodeASN1' ASN1.DER
|
|
|
|
|
|
|
|
derToAsn1 :: BS.ByteString -> Either ASN1.ASN1Error [ASN1.ASN1]
|
|
|
|
derToAsn1 = ASN1.decodeASN1' ASN1.DER
|
|
|
|
|
|
|
|
getUnicodeField :: [ASN1.ASN1] -> String
|
2020-05-19 12:29:15 +02:00
|
|
|
getUnicodeField (ASN1.Start ASN1.Sequence : _ : ASN1.ASN1String strASN1 : _) = fromMaybe "" $ ASN1.asn1CharacterToString strASN1
|
2020-04-26 17:40:25 +02:00
|
|
|
|
|
|
|
testParser :: ASN1P.ParseASN1 String
|
|
|
|
testParser = do
|
2020-04-26 21:06:52 +02:00
|
|
|
foo <- ASN1P.onNextContainer ASN1.Sequence getAll
|
2020-05-19 12:29:15 +02:00
|
|
|
pure $ show foo
|
2020-04-26 21:06:52 +02:00
|
|
|
|
|
|
|
getAll :: ASN1P.ParseASN1 [ASN1.ASN1]
|
|
|
|
getAll = ASN1P.getMany ASN1P.getNext
|
|
|
|
|
2020-04-26 17:40:25 +02:00
|
|
|
--stringParser asn1obj =
|
|
|
|
-- fmap (fromMaybe "" $ ASN1.asn1CharacterToString) ASN1P.getNext
|
|
|
|
|
2020-04-21 14:21:36 +02:00
|
|
|
main = do
|
|
|
|
print someASN1
|
2020-04-26 17:40:25 +02:00
|
|
|
print $ asn1AsDer someASN1
|
|
|
|
print $ derToAsn1 . asn1AsDer $ someASN1
|
|
|
|
putStrLn $ getUnicodeField someASN1
|
|
|
|
print $ ASN1.decodeASN1Repr' ASN1.DER $ asn1AsDer someASN1
|
|
|
|
putStrLn "\nLet's try a real parser combinator:"
|
|
|
|
print $ ASN1P.runParseASN1State testParser someASN1
|